C语言问题,请问哪里错了呢?谢谢

2025-05-21 21:19:27
推荐回答(1个)
回答(1):

结构体定义后面不能直接定义指针*p。

如果需要定义一个结构体的指针,必须要使用malloc分配内存空间。否则就会出错。

#include 
#include 
typedef struct student
{
    char name[20];
    char num[20];
} student;

int main()
{
    student a;
    struct student *p = ( struct student *) malloc(sizeof(student));
    gets(a.name);
    puts(a.name);
    fflush(stdin);
    gets(p->name);
    puts(p->name);
    return 0;
}