Can';t使结构在C中工作(使用GCC)
所以我只是在练习C语言的结构,我遇到了一个大问题。。。我附加的代码和错误 我试图运行的代码是在结构中放入一个预定义值,然后打印它 代码Can';t使结构在C中工作(使用GCC),c,gcc,data-structures,cs50,C,Gcc,Data Structures,Cs50,所以我只是在练习C语言的结构,我遇到了一个大问题。。。我附加的代码和错误 我试图运行的代码是在结构中放入一个预定义值,然后打印它 代码 #include <stdio.h> struct student { int sroll; char sname[20]; int stotal; } int main() { struct student st; clrscr(); st.sroll = 1; strcpy(sname, &
#include <stdio.h>
struct student
{
int sroll;
char sname[20];
int stotal;
}
int main()
{
struct student st;
clrscr();
st.sroll = 1;
strcpy(sname, "Example");
st.stotal = 700
printf("\n Roll => %d", st.sroll);
printf("\n Name => %d", st.sname);
printf("\n Total Marks => %d", st.stotal);
return 0;
}
#包括
结构学生
{
int sroll;
charsname[20];
int stotal;
}
int main()
{
结构学生;
clrsc();
st.sroll=1;
strcpy(sname,“示例”);
圣斯托尔=700
printf(“\n Roll=>%d”,st.sroll);
printf(“\n Name=>%d”,st.sname);
printf(“\n总分=>%d”,圣斯托尔);
返回0;
}
错误
$ gcc -o strpre strpre.c
strpre.c:8:1: error: expected ‘;’, identifier or ‘(’ before ‘int’
8 | int main()
| ^~~
strpre.c: In function ‘main’:
strpre.c:11:5: warning: implicit declaration of function ‘clrscr’ [-Wimplicit-function-declaration]
11 | clrscr();
| ^~~~~~
strpre.c:13:5: warning: implicit declaration of function ‘strcpy’ [-Wimplicit-function-declaration]
13 | strcpy(sname, "Example");
| ^~~~~~
strpre.c:13:5: warning: incompatible implicit declaration of built-in function ‘strcpy’
strpre.c:2:1: note: include ‘<string.h>’ or provide a declaration of ‘strcpy’
1 | #include <stdio.h>
+++ |+#include <string.h>
2 | struct student
strpre.c:13:12: error: ‘sname’ undeclared (first use in this function); did you mean ‘rename’?
13 | strcpy(sname, "Example");
| ^~~~~
| rename
strpre.c:13:12: note: each undeclared identifier is reported only once for each function it appears in
strpre.c:14:20: error: expected ‘;’ before ‘printf’
14 | st.stotal = 700
| ^
| ;
15 | printf("\n Roll => %d", st.sroll);
| ~~~~~~
strpre.c:16:25: warning: format ‘%d’ expects argument of type ‘int’, but argument 2 has type ‘char *’ [-Wformat=]
16 | printf("\n Name => %d", st.sname);
| ~^ ~~~~~~~~
| | |
| int char *
| %s
$gcc-o strpre strpre.c
strpre.c:8:1:错误:应为“;”,标识符或“(”在“int”之前)
8 | int main()
| ^~~
strpre.c:在函数“main”中:
strpre.c:11:5:警告:函数“clrsc”的隐式声明[-Wimplicit函数声明]
11 | clrsc();
| ^~~~~~
strpre.c:13:5:警告:函数“strcpy”的隐式声明[-Wimplicit函数声明]
13 | strcpy(sname,“示例”);
| ^~~~~~
strpre.c:13:5:警告:内置函数“strcpy”的隐式声明不兼容
strpre.c:2:1:注意:包括“”或提供“strcpy”声明
1 |#包括
+++|+#包括
2 |结构学生
strpre.c:13:12:错误:“sname”未声明(此函数首次使用);您的意思是“重命名”吗?
13 | strcpy(sname,“示例”);
| ^~~~~
|改名
strpre.c:13:12:注意:每个未声明的标识符对于它出现在其中的每个函数只报告一次
strpre.c:14:20:错误:“printf”之前应为“;”
14 | st.stotal=700
| ^
| ;
15 | printf(“\n Roll=>%d”,st.sroll);
| ~~~~~~
strpre.c:16:25:警告:格式“%d”要求参数类型为“int”,但参数2的类型为“char*”[-Wformat=]
16 | printf(“\n Name=>%d”,st.sname);
| ~^ ~~~~~~~~
| | |
|整型字符*
|%s
顺便说一下,我正在使用CS50IDE和最新的GCC版本
编辑:在我尝试了@Robert的修复之后…它仍然不起作用。。。
我想你漏掉了一个分号:
struct student
{
int sroll;
char sname[20];
int stotal;
}; // <========================= missing ;
你的意思是
strcpy(st.sname,Example);
而不是strcpy(sname,Example);
当你在C++中的C或类声明中丢失了一个分号后,GCC中的错误消息就不太有意义了,很难分析。另一个错误:<代码> ST.StoTale= 700 :你需要分号,你不太喜欢它们?为什么GCC自己不加?TW您也忘记了#包括
strcpy(st.sname, "Example");