繁体   English   中英

结构定义和编译器错误有什么问题?

[英]What is the problem here in struct definition and compiler error?

当我键入以下代码时:

struct student {
    int* grades;
    int num_grades;
};


int main()
{
    struct student s1 = { { 100, **50**, 74}, 3 }, s2 = { {100, **90**, 80, 90, 90}, 5 }, s3 = { {85, **90**}, 2 };
    struct student arr[3] = { s1, s2, s3 };
}

我收到两个错误错误:

C2078 too many initializers
E0146 too many initializer values

我用两个粗体星号标记了数字,以告诉您 Visual Studio 将红色下划线放在哪里。

有谁知道为什么编译器告诉我这个错误?

您不能通过大括号括起来的初始化列表来初始化指针( grades变量)。 您需要使用复合文字

就像是

 struct student s1 = { (int []){ 100,  50 , 74}, 3 };

应该做的工作。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM