标题: 小编程题,一起来分享! [打印本页] 作者: jazzforlove 时间: 2015-10-29 00:09 标题: 小编程题,一起来分享! /*用结构体定义一个三口之家,家里每个成员都有:姓名,年龄,身份(如父亲),打印这个三口之家的所有成员的信息*/
#include <stdio.h>
int main(){
struct Person{
char *name;
int age;
char *status;
};
struct Person f={"成龙",60,"父亲"};
struct Person m={"林凤娇",55,"母亲"};
struct Person s={"房祖名",30,"儿子"};
struct Person pers[3]={f,m,s};
for (int i=0; i<3; i++) {
struct Person *p=pers+i;
printf("name=%s,age=%d,status=%s\n",(*p).name,(*p).age,(*p).status);
}
return 0;
}作者: c9527 时间: 2015-10-29 09:47
可以这样简化
#include <stdio.h>
int main(){
typedef struct{
char *name;
int age;
char *status;
}Person;
Person f={"成龙",60,"父亲"};
Person m={"林凤娇",55,"母亲"};
Person s={"房祖名",30,"儿子"};
Person pers[3]={f,m,s};
for (int i=0; i<3; i++) {
struct Person *p=pers+i;
printf("name=%s,age=%d,status=%s\n",(*p).name,(*p).age,(*p).status);
}
return 0;
}