首页 > 解决方案 > 如何使用 structs 数组制作玩家列表?

问题描述

我是结构的新手。我正在制作一个程序,我想在其中建立一个带有 structs 的玩家列表。这是我写到现在的。是意大利语的。但基本上,当我只插入一个播放器时,程序运行良好。当我想添加多个时它不会。

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define MAX 25

struct rol{
    int position[3];
    int lato[1];
};
typedef struct rol role;

struct pl{
    role *ruol;
    int shirt_num;
    char name[20];
    char lastn[20];
};
typedef struct pl player;

struct tm{
    int classpos;
    int pnt;
    player players[MAX];
};


typedef struct tm team;

void define_role (role *ruolo);
void make_pl(player *pl);

int main(int argc, char** argv) {
    int risp;
    player *giocatore;
    int cont;
    printf("Vuoi effettuare lavori su un giocatore o su una squadra ? \n [ 1) giocatore  2)  squadra] ");
    scanf("%d",&risp);
    if(risp==1){
    printf("\n\n1)Crea Giocatore \n 2)Modifica giocatore \n3)Inserisci giocatore in una squadra");
    printf("\n 4)Visualizza dati giocatore \n ");
    scanf("%d",&risp);
    if(risp==1){
        cont++;

    giocatore = (player*)malloc (cont*sizeof(player));
    make_pl(&giocatore[cont-1]); //this is the main problem basically 
    }
    }
    return 0;
}

void make_pl(player *pl){
    printf("\n Inserisci nome ");
    scanf("%s",pl->name);
    printf("\n Inserisci cognome ");
    scanf("%s",pl->lastn);
    define_role(pl->ruol);
    printf("Inserisci numero di maglia ");
    scanf("%d",&pl->shirt_num);
    } 

void define_role(role *ruolo){
    printf("\n Inserisci lato ");
    scanf("%d",&ruolo->lato);
    printf("\n Inserisci posizione ");
    scanf("%d",&ruolo->position);

    }

我以常量 cont =1 运行,它的工作原理很明显。当我升级该值时,因为我想在它进入函数定义角色时添加一个新播放器,它会在第一次 scanf 之后阻塞。

scanf("%d",&ruolo->lato);

它说退出错误bla bla bla。

我只想列出一份球员名单,列出他们的技术特点。我不知道有什么问题。当我构建它时,它没有给我任何错误。

标签: cstruct

解决方案


我检查了你的代码,我发现了一些问题,当你使用scanf()这个函数时,从输入中读取每个字符,直到它找到一个空格但不读取换行符,所以,你需要清理输入或者只是放一个格式化字符串前的空格:scanf(" %d", &somVariable);. 您需要为结构role内部的结构分配内存player,因此在您的函数define_role中不会出现分段错误,您还需要为您的 ```cont`` 变量添加一个值。我将代码与此更正一起添加,并添加了一段时间以添加更多玩家。

已编辑

我阅读了您的评论,我没有注意到make_pl(&giocatore[cont-1]);您将gicatore其视为数组,但它是播放器结构的指针,因此当您尝试添加第二个播放器时,程序以错误结束。因此解决方案是创建一个数组或链表来保存您添加的播放器。数组的主要问题是,如果您尝试超过该限制,您只能添加特定数量的玩家,程序将以错误结束,因此我建议您使用链表。

我设置scanf("%d",&ruolo->lato[index])为 0 是因为 lato 在你的结构角色中是一个包含一个 int 的数组,你可以声明一个 int 而不是一个只有一个 int 的数组,所以要访问数组的第一个元素,你需要将索引设置为 0,如果你写,例如,120 这个数字它将保存在 lato[0]

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define MAX 25

struct rol{
    int position[3];
    int lato[1];
};
typedef struct rol role;

struct pl{
    role *ruol;
    int shirt_num;
    char name[20];
    char lastn[20];
};
typedef struct pl player;

struct tm{
    int classpos;
    int pnt;
    player players[MAX];
};


typedef struct tm team;

void define_role (role *ruolo);
void make_pl(player *pl);

int main(int argc, char** argv) {
    int risp;
    player *giocatore;
    player players[MAX];
    int cont = 0;
    printf("Vuoi effettuare lavori su un giocatore o su una squadra ? \n [ 1) giocatore  2)  squadra] ");
    scanf(" %d",&risp);

    if(risp==1) {
        do {
            printf("\n\n1)Crea Giocatore\n2)Modifica giocatore\n3)Inserisci giocatore in una squadra");
            printf("\n4)Visualizza dati giocatore\n 5) Finish program\n");
            scanf(" %d",&risp);
            if(risp==1) {
                cont++;
                giocatore = (player*)malloc (cont*sizeof(player));
                giocatore->ruol = (role*)malloc(sizeof(role));
                make_pl(giocatore); //this is the main problem basically 
                players[cont-1] = *giocatore;
            }
        } while(risp != 5);
    }
    return 0;
}

void make_pl(player *pl) {
    printf("\n Inserisci nome ");
    scanf(" %s",pl->name);
    printf("\n Inserisci cognome ");
    scanf(" %s",pl->lastn);
    define_role(pl->ruol);
    printf("Inserisci numero di maglia ");
    scanf(" %d",&pl->shirt_num);
} 

void define_role(role *ruolo) {
    printf("\n Inserisci lato ");
    scanf(" %d",&ruolo->lato[0]);
    printf("\n Inserisci posizione ");
    scanf(" %d",&ruolo->position[0]);
}

推荐阅读