C允许把一个结构赋值给另一个结构,不能对数组这样做。
下面操作都是允许的:
struct stu a =
{
.num = 1,
.name = "xiaowang",
.score = 80,
};
struct stu b;
b = a; //赋值
struct stu c = a; //初始化 把一个结构初始化为另一个结构
#include <stdio.h>
#include <stdlib.h>
struct stu
{
int num;
char name[20];
int score;
};
void show(struct stu a)
{
printf("the num is =%d\n",a.num);
printf("the name is =%s\n",a.name);
printf("the score is =%d\n",a.score);
}
void fun(void)
{
struct stu a =
{
.num = 1,
.name = "xiaowang",
.score = 80,
};
struct stu b;
b = a; //赋值
struct stu c = a; //初始化
show(a);
show(b);
show(c);
}
void main(void)
{
fun();
}
执行结果: