微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

不能使结构在 C 中工作使用 GCC

如何解决不能使结构在 C 中工作使用 GCC

所以我只是在 C 中练习结构,我遇到了一个大问题......我附上了代码错误 我试图运行的代码是在结构中放入一个预定义的值,然后打印出来。

代码

#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;
}

错误

$ 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 版本的 CS50 IDE。

编辑:在我尝试了@Robert 的修复之后......它仍然无法正常工作...... https://pastebin.ubuntu.com/p/mBjkR292wb/

解决方法

我想你漏掉了一个分号:

struct student
{
    int sroll;
    char sname[20];
    int stotal;
}; // <========================= missing ;

正如评论中所写,您还有另一个拼写错误:

strcpy(st.sname,"Example");

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。