为了账号安全,请及时绑定邮箱和手机立即绑定

结构体赋值

结构体赋值

C
精慕门0141219 2017-12-09 18:25:36
假设现在有结构体struct student  student1, student2//结构体变量名我能不能先申明,然后再初始化呢,上面是申明,下面再student1 = {};student2 = {}在编译的时候出现error:expected expression before ‘{’token不知道为什么,有人能够解释一下其中的原理吗?代码如下:#include<stdio.h>typedef struct weapon{int price;int rtk;char name[20];struct weapon *next;}W;int main(){W *head;W w1,w2,w3; w1 ={100,200,"ijnjin",NULL}; w2 ={1400,24400,"ifafadfjnjin",NULL}; w3 ={13400,204440,"ifdfsdfsdjnjin",NULL};head = &w1;W *p = head;while(p!=NULL){printf("the price is %d,the nane is %s,the rtk is %d\n",p->price,p->name,p->rtk);p = p->next;}return 0;}
查看完整描述

4 回答

?
慕用4063026

TA贡献195条经验 获得超115个赞

W w1 ={100,200,"ijnjin",NULL};
W w2 ={1400,24400,"ifafadfjnjin",NULL};
W w3 ={13400,204440,"ifdfsdfsdjnjin",NULL};

输出:the price is 100,the nane is ijnjin,the rtk is 200

单独定义就行了

查看完整回答
反对 回复 2017-12-10
?
精慕门0141219

TA贡献1条经验 获得超0个赞

#include<stdio.h>


typedef struct weapon{

int price;

int rtk;

char name[20];


struct weapon *next;

}W;


int main()

{

W *head;

W w1,w2,w3;

 w1 ={100,200,"ijnjin",NULL};

 w2 ={1400,24400,"ifafadfjnjin",NULL};

 w3 ={13400,204440,"ifdfsdfsdjnjin",NULL};

head = &w1;

W *p = head;

while(p!=NULL){

printf("the price is %d,the nane is %s,the rtk is %d\n",p->price,p->name,p->rtk);

p = p->next;

}

return 0;

}


查看完整回答
反对 回复 2017-12-10
?
慕用4063026

TA贡献195条经验 获得超115个赞

当然可以,那个报错是声明结构体变量名的问题吧,最好你把你报错的代码发出来看看

查看完整回答
反对 回复 2017-12-10
?
成为优秀的人

TA贡献1条经验 获得超0个赞

初学者,能力有限。。。

查看完整回答
反对 回复 2017-12-09
  • 4 回答
  • 0 关注
  • 2042 浏览

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信