如何通过函数为typedef别名指针赋值?

时间:2019-05-15 11:10:10

标签: c

我想在一个函数中创建一个新的championship,但是每当我给它赋值时,代码就会建立,但是在运行时,它会在此部分停止。

typedef struct
{
    char id[10];
    int c;
}team;

typedef struct
{
    team t1;
    team t2;
    int t1g;
    int t2g;
}match;

typedef struct
{
  match matches[30];
  team teams[6];
  int teamno;
  int played;
}* championship;

championship newchampionship(){
    championship temp;
    temp->played=0;
    temp->teamno=0;
    return temp;
}

int main(){
   championship ch1=newchampionship();
   ...
}

只要到达temp->played=0;,整个过程就会停止,并且过程将返回-1073741819。

有人知道如何解决此问题吗?我只想要一个带有空数组且int为0的冠军。

1 个答案:

答案 0 :(得分:-1)

如下更改代码:

typedef struct
{
  match matches[30];
  team teams[6];
  int teamno;
  int played;
} championship;

championship *newchampionship(){
    championship *temp = malloc(sizeof championship);
    temp->played=0;
    temp->teamno=0;
    return temp;
}

int main(){
   championship *ch1=newchampionship();
   ...
}