如何给结构体成员赋值问题

zaixiankaifa 2013-09-30 12:24:47
#include <stdio.h>

int main()
{
struct student{
char name[20];
int score;
};

struct student st={"John",100},*ptr;
// student st={"John",100},*ptr;
ptr=&st;

printf("ptr->name=%s\n",ptr->name);

return 0;
}

用gcc 4.2编译器无法编译:// student st={"John",100},*ptr;,但是用VS08却可以, 是不是编译器问题?
另外用gcc 4.2也无法这样编译:st={"John",100};是不是只能这样赋值:struct student st={"John",100};或者:st.name="John";st.score=100; ?
...全文
1265 10 打赏 收藏 转发到动态 举报
写回复
用AI写文章
10 条回复
切换为时间正序
请发表友善的回复…
发表回复
max_min_ 2013-09-30
  • 打赏
  • 举报
回复
没有错误吧! 楼主 clean下,再make试试
qq120848369 2013-09-30
  • 打赏
  • 举报
回复
gcc4.6无压力。
ChristopherWu 2013-09-30
  • 打赏
  • 举报
回复
vs08全都没有问题(包括注释了的——)
pathletboy 2013-09-30
  • 打赏
  • 举报
回复
codeblock with mingw-gcc 4.6.2没有问题。
lxmuyu 2013-09-30
  • 打赏
  • 举报
回复
别太在意不同编译器的结果不同,毕竟不是搞编译器的。。。。
AnYidan 2013-09-30
  • 打赏
  • 举报
回复
struct point { int x; int y; }; The keyword struct introduces a structure declaration, which is a list of declarations enclosed in braces. An optional name called a structure tag may follow the word struct (as with point here). The tag names this kind of structure, and can be used subsequently as a shorthand for the part of the declaration in braces. 那是初始化,不是赋值
猫仔- 2013-09-30
  • 打赏
  • 举报
回复
学习了!
cyldf 2013-09-30
  • 打赏
  • 举报
回复
struct student st={"John",100}; 只能在结构体定义的地方这样初始化; 如果在其他地方,就只能逐个成员初始化了,所以 st={"John",100}是不行的
citycal 2013-09-30
  • 打赏
  • 举报
回复
1.没见过 student st={"John",100},*ptr;的写法。不过在vs2010上可以通过。你说的不能在gcc4.2上通过,肯定是编译器的问题,我没有4.2的gcc所以没试,可以找个4.2的机子再试试,看是否是因为缺少了什么。应该还可以通过gcc网站查看不同的版本有哪些不同。 2.我了解的赋值方式就是你说的struct student st={"John",100};或者:st.name="John";st.score=100;
图灵狗 2013-09-30
  • 打赏
  • 举报
回复
C语言标准是需要struct student st带struct定义结构体变量的,student st的话并不是所有编译器都支持的,如果一定要用student st的话,那么最好用typedef进行定义: typedef struct student{ char name[20]; int score; }student;
引用 楼主 zaixiankaifa 的回复:
#include <stdio.h> int main() { struct student{ char name[20]; int score; }; struct student st={"John",100},*ptr; // student st={"John",100},*ptr; ptr=&st; printf("ptr->name=%s\n",ptr->name); return 0; } 用gcc 4.2编译器无法编译:// student st={"John",100},*ptr;,但是用VS08却可以, 是不是编译器问题? 另外用gcc 4.2也无法这样编译:st={"John",100};是不是只能这样赋值:struct student st={"John",100};或者:st.name="John";st.score=100; ?

69,371

社区成员

发帖
与我相关
我的任务
社区描述
C语言相关问题讨论
社区管理员
  • C语言
  • 花神庙码农
  • 架构师李肯
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧