给结构体中数组成员赋值
#include<stdio.h>#include<string.h>
struct student
{
int num;
char name;
float score;
};
void print(struct student);
void main()
{
struct student stu;
stu.num=8;
stu.name={'s','f'};
stu.score={98.5,99.0,99.5};
print(stu);
}
void print(struct student stu)
{
printf("\tnum :%d\n",stu.num);
printf("\tname :%s\n",stu.name);
printf("\tscore:%5.2f\n",stu.score);
printf("\tscore :%5.2f\n",stu.score);
printf("\tscore :%5.2f\n",stu.score);
printf("\n");
}
红色部分编译是出现错误,小甲鱼给score数组赋值是一个一个复制的
void main()
{
struct student stu;
stu.num=8;
strcpy(stu.name,"Fishc.com!");
stu.score=98.5;
stu.score=99.5;
stu.score=99.0;
print(stu);
}
为啥非要对score数组一个一个赋值
“为啥非要对score数组一个一个赋值”
因为C语言没有这样的语法
stu.name={'s','f'};
stu.score={98.5,99.0,99.5}; 只有在初始化的时候允许这样赋值 感谢两位的回答
非常感谢
页:
[1]