#include<stdio.h>
#include<stdlib.h>
struct student{
//定义一个学生结构体
char name[30];
int age;
}stu = {"zhangsan", 30};
int main(){
struct student *pStudent;
struct student *ps;
ps = (student *)malloc(sizeof(struct student));
return 0;
}
在上面的简单代码中,本来是为了测试malloc方法的使用,结果碰到这个错误,不过倒是挺好,平时经常遇到!
总结为一句:总之有碰到定义结构体类型的时候,struct关键字绝对不能少。
像上面的代码部分,错误就出在动态分配内存的那条语句,应该改成:
ps = (struct student *)malloc(sizeof(struct student));
值得提出的是,这函数的返回值为指针类型,具体指针类型为何值,不确定,必须进行强制转换。