首页 > 解决方案 > 试图在该类型的数组中添加某种类型的变量

问题描述

所以我想创建一个名为 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:score2likea 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++;
        }

    }
}

标签: carrayspointersstructure

解决方案


你感到困惑,我并不感到惊讶。

正如 Christian Gibbons、Barmar 和 user12986714 所说,jogo必须是您的jogo结构,而不是指向jogo. 我想你在某个阶段改变了,} jogo;因为}* 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;

typedef struct jogo2
{
   int ident;/*idp of a product*/
   char nome[MAX_CHARS]; /* string that describes a team eg. Barcelona */
   char *equipas[2];
   int score[2];
} jogo2;

int main() {

   printf("sizeof jogo %d\n",sizeof(jogo));
   printf("sizeof jogo2 %d\n",sizeof(jogo2));
   return 0;
}

如您所见jogo,具有指针大小并jogo2具有结构的大小。

此外,您的代码中存在各种问题。代码中直接对所有内容进行了简要注释。不要犹豫,提出问题。

#include<stdlib.h>
#include<stdio.h>
#include <string.h>
#define MAX_CHARS 1024
#define MAX_SIZE 5

int line = 1;
// static int size = MAX_SIZE; // Not useful. It is the same than MAX_SIZE
// int i = 0; this variable is not really used
int size_until = 0;

typedef struct jogo
{
   // int ident; Never used
   char nome[MAX_CHARS];
   char equipas[2][1024]; // equipas is an array of two strings. If you use char *equipas[2], you will have to alloc memory for each string
   int score[2];
} jogo; // * has been removed

jogo **sistema_jog; //sistema_jog is an array of pointer to jogo struct. You allocate it after.
// you could also have an array of jogo struct but it would need more modifications in your code.
// I suppose the confusion is here. To train, you could try to do : jogo * sistema_jog and to modify what it is needed in your code.

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); // *: s is a pointer to jogo struct. See comment on sistema_jog declaration

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*)); // Each element of sistema_jog is a pointer to a jogo struct
   while ((c = getchar())!= 'x') {
       switch (c)
       {
          case 'a':
          {
             scanf("%1023[^:\n]:%1023[^:\n]:%1023[^:\n]:%d:%d",nome_jg,eq1,eq2,&pont1,&pont2); // be carefull, see % and 1023 in the third field of your code
    //         i++; not used elsewhere
             a(nome_jg,eq1,eq2,pont1,pont2);
             break;
          }
       }
   }
   // Only to check
   for (int i=0; i<size_until;i++)
        printf ("%s:%s:%s:%d:%d\n",
                sistema_jog[i]->nome,
                sistema_jog[i]->equipas[0],
                sistema_jog[i]->equipas[1],
                sistema_jog[i]->score[0],
                sistema_jog[i]->score[1]);
   return 0;
}

int nome_in(char nome[])
{
    int i;
    for(i=0; i < size_until; i++) // size_until : You have to check only elements that exist either you have less or more elements than size (static int = MAX_SIZE)
    {
        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_until;i++) // size_until : Idem as above
    {
        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) // * : s is a pointer to jogo struct
{
    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);
    } */ // I do not understand the objective of this test. So, I commented it. But it is not the question
    else
    {
        jogo* novo_jogo =  (jogo*) malloc(sizeof(jogo));
        cria_jogo(novo_jogo,nome,team1,team2,score1,score2);
        if (size_until < MAX_SIZE) // = has been removed. Index of array goes from 0 to size of array-1
        {
            sistema_jog[size_until] = novo_jogo;
            size_until++;
        }
        else
        {
            sistema_jog = (jogo**) realloc(sistema_jog, sizeof(jogo**)*(size_until+1)); // *: see comment on sistema_jog declaration, +1:  array index goes from 0 to size-1
            // Remark : It is not efficient to realloc one by one. It would better to realloc MAX_SIZE by MAX_SIZE. You could try to do it
            sistema_jog[size_until] = novo_jogo;
            size_until++;
        }
    }
}

推荐阅读