【发布时间】:2022-01-04 17:44:34
【问题描述】:
我正在尝试用 C 语言实现一个结构。我遇到了这些错误。
请帮我解决这个错误,并向我解释我做错了什么。
main.c:7:12: error: variable ‘s1’ has initializer but incomplete type
7 | struct student s1 = {"Nick",16,50,72.5};
| ^~~~~~~
main.c:7:26: warning: excess elements in struct initializer
7 | struct student s1 = {"Nick",16,50,72.5};
| ^~~~~~
main.c:7:26: note: (near initialization for ‘s1’)
main.c:7:33: warning: excess elements in struct initializer
7 | struct student s1 = {"Nick",16,50,72.5};
| ^~
main.c:7:33: note: (near initialization for ‘s1’)
main.c:7:36: warning: excess elements in struct initializer
7 | struct student s1 = {"Nick",16,50,72.5};
| ^~
main.c:7:36: note: (near initialization for ‘s1’)
main.c:7:39: warning: excess elements in struct initializer
7 | struct student s1 = {"Nick",16,50,72.5};
| ^~~~
main.c:7:39: note: (near initialization for ‘s1’)
main.c:7:20: error: storage size of ‘s1’ isn’t known
7 | struct student s1 = {"Nick",16,50,72.5};
我的代码
#include<stdio.h>
#include<stdlib.h>
int main()
{
struct student s1 = {"Nick",16,50,72.5};
printf("%s",s1.name);
// return 0;
}
struct student{
char name[4];
int age;
int roll_no;
float marks;
}s1;
【问题讨论】:
-
编译器提示,您在使用前还没有定义
struct student的成员。另外:char name[4];对于“Nick”来说太短了,几乎是任何名。
标签: c struct declaration definition incomplete-type