【C语言基础】结构体数组的易错问题


今天在学习结构体数组的时候,自己写了一份代码

#include 

struct Student {
    //年龄
    char *name;
    //编号
    char *id;
    //年龄
    unsigned age;
};

int main(){
    struct Student students[100] = {
        {"小明","15061",11},
        {"小红","15062",11},
        {"小童","15063",10}
    };
    
    for (int i = 0; i

编译时提示了以下错误

a.c:19:47: error: use of undeclared identifier 'Student'; did you mean
      'students'?
    for (int i = 0; i

后来找啊找找啊找,找到大脑瞬间爆炸,发现是因为sizeof这个函数在获取结构体类型大小时,需要在结构体名前加上 struct 修饰,

所以正确的代码应该为:

#include 

struct Student {
    //年龄
    char *name;
    //编号
    char *id;
    //年龄
    unsigned age;
};

int main(){
    struct Student students[100] = {
        {"小明","15061",11},
        {"小红","15062",11},
        {"小童","15063",10}
    };
    
    for (int i = 0; i



你可能感兴趣的:(ios)