所以我想创建一个我称之为jogo的结构的数组
结构:
typedef struct jogo
{
int ident;/*idp of a product*/
char nome[1024]; /* string that describes a team eg. Barcelona */
char *equipas[2]; /*array of strings like {"Barcelona","Madrid"}*/
int score[2]; /*array of strings like {"Barcelona","Madrid"}*/
}* jogo;
我想创建一个没有特定大小的数组来存储jogo类型的变量。]>
[当我像a nome:equipa1:equipa2_score1:score2
一样键入(添加)a elclassico:barcelona:madrid:1:0
时,我想创建一个jogo类型的变量并将其存储在数组sistema_jog
中。
如果我存储了一些东西并且数组已满,我想重新分配数组的大小以存储更多jogo类型的变量。]>
但是由于某些原因,当我尝试这样做时,我总是会得到分段错误核心,而我不知道为什么。
程序:
#include<stdlib.h> #include<stdio.h> #include <string.h> #define MAX_CHARS 1024 /* max characters of a word */ #define MAX_SIZE 5 int line = 1; /* counts the number of lines of the stdin */ static int size = MAX_SIZE; int i = 0; /*ident of the variable jogo*/ int size_until = 0; typedef struct jogo { int ident;/*idp of a product*/ char nome[MAX_CHARS]; /* string that describes a team eg. Barcelona */ char *equipas[2]; int score[2]; }* jogo; jogo *sistema_jog; void a(char nome[],char team1[],char team2[],int score1,int score2); int team_not_in(char team1[],char team2[]); int nome_in(char nome[]); void cria_jogo(jogo s,char nome[],char equipa1[],char equipa2[],int score1,int score2); int main() { char c; char nome_jg[MAX_CHARS]; char eq1[MAX_CHARS]; char eq2[MAX_CHARS]; int pont1; int pont2; sistema_jog = (jogo*) calloc(MAX_SIZE,sizeof(jogo)); while ((c = getchar())!= 'x') { switch (c) { case 'a': { scanf("%1023[^:\n]:%1023[^:\n]:1023%[^:\n]:%d:%d",nome_jg,eq1,eq2,&pont1,&pont2); i++; printf("nome: %s",sistema_jog[0]->nome); //a(nome_jg,eq1,eq2,pont1,pont2); break; } } } return 0; } int nome_in(char nome[]) { int i; for(i=0; i < size; i++) { if (strcmp(sistema_jog[i]->nome,nome) == 0) return 1; } return 0; } int team_not_in(char team1[],char team2[]) { int i; for (i=0;i<size;i++) { if((strcmp(sistema_jog[i]->equipas[0],team1) != 0) || (strcmp(sistema_jog[i]->equipas[1],team2) != 0)) return 1; } return 0; } void cria_jogo(jogo s,char nome[],char equipa1[],char equipa2[],int score1,int score2) { strcpy(s->nome,nome); strcpy(s->equipas[0],equipa1); strcpy(s->equipas[1],equipa2); s->score[0] = score1; s->score[1] = score2; } void a(char nome[],char team1[],char team2[],int score1,int score2) { int NL = line; if (nome_in(nome) == 1) printf("%d Jogo existente.",NL); else if (team_not_in(team1,team2) == 0) { printf("%d Equipa existente.",NL); } else { jogo novo_jogo = (jogo) calloc(sizeof(jogo),sizeof(jogo)); cria_jogo(novo_jogo,nome,team1,team2,score1,score2); if (size_until <= MAX_SIZE) { sistema_jog[size_until] = novo_jogo; size_until++; } else { sistema_jog = (jogo*) realloc(system, sizeof(jogo)*size_until); sistema_jog[size_until] = novo_jogo; size_until++; } } }
所以我想创建一个由jogo结构构成的数组:typedef struct jogo {int ident; / *产品的idp * / char nome [1024]; / *描述团队的字符串,例如。 ...
您感到困惑,我并不感到惊讶。
[正如Christian Gibbons,Barmar和user12986714所说,jogo
必须是您的jogo
结构,而不是指向jogo
的指针。我以为您由于编译错误而在某个阶段将} jogo;
更改为}* jogo;
。但是,这不是最初的问题,当您感到困惑之后。