結構體變數賦值及成員引用
阿新 • • 發佈:2019-01-08
#include<stdio.h>
#include<string.h>
struct person
{
char name[12];
int age;
char sex;
float height;
float weight;
};
int main(void)
{
struct person p1={"ZhangSan",18,'M',1.75,70.5},p2,p3;
printf("姓名 年齡 性別 身高 體重:");
scanf("%s %d %c %f %f",p2.name,&p2.age,&p2.sex,&p2.height,&p2.weight);
p3=p1;
strcpy(p3.name,"WangMing");
p3.height=1.72;
p3.weight=68;
printf("姓名 年齡 性別 身高 體重\n");
printf("%s %d %5c %10f %10f\n",p1.name,p1.age,p1.sex,p1.height,p1.weight);
printf("%s %d %5c %10f %10f\n",p2.name,p2.age,p2.sex,p2.height,p2.weight);
printf("%s %d %5c %10f %10f\n",p3.name,p3.age,p3.sex,p3.height,p3.weight);
return 0;
}
#include<string.h>
struct person
{
char name[12];
int age;
char sex;
float height;
float weight;
};
int main(void)
{
struct person p1={"ZhangSan",18,'M',1.75,70.5},p2,p3;
printf("姓名 年齡 性別 身高 體重:");
scanf("%s %d %c %f %f",p2.name,&p2.age,&p2.sex,&p2.height,&p2.weight);
p3=p1;
strcpy(p3.name,"WangMing");
p3.height=1.72;
p3.weight=68;
printf("姓名 年齡 性別 身高 體重\n");
printf("%s %d %5c %10f %10f\n",p1.name,p1.age,p1.sex,p1.height,p1.weight);
printf("%s %d %5c %10f %10f\n",p2.name,p2.age,p2.sex,p2.height,p2.weight);
printf("%s %d %5c %10f %10f\n",p3.name,p3.age,p3.sex,p3.height,p3.weight);
return 0;
}