首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 开发语言 > C语言 >

对一个结构体数组进行初始化,但是有点疑惑解决思路

2012-03-08 
对一个结构体数组进行初始化,但是有点疑惑#include stdio.hstruct stu{int numberchar *namechar *sex

对一个结构体数组进行初始化,但是有点疑惑
#include <stdio.h>

struct stu
{
int number;
char *name;
char *sex;
int socre;

}student[5] ;


struct stu student[5] = {{1,"wangfang","girl",68}
,{2,"xiao xi","girl",45}
,{3,"lin ye","boy",58}
,{4,"kobe","boy",34}
,{5,"xiao xia","girl",90}};

int main(void)
{  
int i,count = 0;
double average,sum = 0;
  printf("The unpass student:");
for(i = 0;i < 5;i ++)
{
sum = sum + student[i].socre;
if(student[i].socre < 60){
printf("student[%d]:%d\n%s\n%s\n%d\n\n\n",i,student[i].number,student[i].name,student[i].sex,student[i].socre);
count++;
}
}
average = sum * 1.0 / 5;
printf("count = %d,average = %f\n",count,average);

  return 0;
}
运行编译之后报错提示是 student redefintion, 重新定义了

对于一般初始化,就不会出现这样的问题,敢问大侠,这到底是怎么一回事?感激不尽

[解决办法]
struct stu
{
int number;
char *name;
char *sex;
int socre;

}student[5] ; // 已经定义了student


//这里又定义了。
struct stu student[5] = {{1,"wangfang","girl",68}
[解决办法]
struct stu
{
int number;
char *name;
char *sex;
int socre;

};

struct stu student[5] = {{1,"wangfang","girl",68}
,{2,"xiao xi","girl",45}
,{3,"lin ye","boy",58}
,{4,"kobe","boy",34}
,{5,"xiao xia","girl",90}};
[解决办法]

探讨
struct stu
{
int number;
char *name;
char *sex;
int socre;

};

struct stu student[5] = {{1,"wangfang","girl",68}
,{2,"xiao xi","girl",45}
,{3,"lin ye","boy",58}
,{4,"kobe","boy",34}
,{5,……

[解决办法]
如楼上,LZ你重复定义了student[5]
[解决办法]
本来你就又定义了,你可以这样做
typedef struct stu
{
int number;
char *name;
char *sex;
int socre;

}student;

strdent st[5];//定义一个struct str类型的数组变量 st,大小为5
[解决办法]
C/C++ code
[color=#FF0000]typedef[/color]struct stu{int number;char *name;char *sex;int socre;}student[5] ;student array = {{1,"wangfang","girl",68},{2,"xiao xi","girl",45},{3,"lin ye","boy",58},{4,"kobe","boy",34},{5,"xiao xia","girl",90}};
[解决办法]
LS都说清楚了,学习,学习。

热点排行
Bad Request.