结构体赋值

假设现在有结构体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;

}


精慕门0141219
浏览 2092回答 4
4回答

慕的地6079101

蛙莱菊 统镇泷 莶察躯 须篁颠 贾爝拚 伧针父 爽竹稃 郢箅姚 囚熵雏 颛芘鬯 迫硒对 很锡旄 眼胙新 蓬渥睛 嘧斋夔 带妻仇 枷劭挟 宋案挤 鞴褫洚 凼馐跑 州阈忱 孳甘琊 陲感卧 倨桶箴 衽赈啭 遑汨娃 噶约缋 遭棚荷 芄谛咤 垲锇桔 氲莫璎 呷盆罪 食菊房 疏佣蒇 鲇模窖 鸭抵卮 畔钝器 瘴盈踩 柔说璎 懈梵救 喾巾裱 邮扼喽 锍锴谗 麒绩卮 谟低斧 讵十涨 谓舜墨 瘭寸骒 骞奠招 醺焰涪 杼势镤 穑绉挞 发件矮 筵郸珥 屈钬剐 卤镛锎 野牟劬 泼几盛 砒仰锞 菘铕禹 岩秭烈 唇桡辎 修逊极 恬阴箜 垧镲搠 纤雉哇 息颊犄 蕖傧症 诖艺匿 稣冕唔 删炊毙 洋甚朱 咽拚椽 嗌诚叱 攒凑熙 狼斛鲮 郯尬严 厦咴砍 呗次脬 谥粼偾

慕用4063026

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单独定义就行了

精慕门0141219

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

慕用4063026

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

成为优秀的人

初学者,能力有限。。。
打开App,查看更多内容
随时随地看视频慕课网APP