结构体经典C语言编程,[编程] C语言的结构体

结构体

struct 结构体名{}变量名;

结构体变量:

struct person{

char *name;

int age;

float score;

} student;

成员的获取和赋值

//Members of the acquisition and assignment

student.name="taoshihan";

student.age=30;

student.score=100;

printf("name=%s n",student.name);

C语言结构体数组

struct stu{

char *name;

int age;

float score;

} classes[5];

遍历结构体数组

struct people{

char *name;

int age;

float score;

} d[]={

{"taoshihan",20,100},

{"lisi",30,90}

};

int len=sizeof(d)/sizeof(d[0]);

printf("d length=%d n",len);

for(int i=0;i

printf("loop...%s,%d,%.1f n",d[i].name,d[i].age,d[i].score);

}

C语言结构体和指针

struct 结构体名*变量名;

struct person1{

char *name;

int age;

float score;

} a={"taoshihan",20,100},*b=&a;

struct person1 *c=&a;

获取结构体成员

printf("b.name=%s n",(*b).name);

printf("c.name=%s n",c->name);

完整代码:

#include

intmain(){

printf("hello world");//Structure variables

structperson{char *name;intage;floatscore;

} student;//Members of the acquisition and assignment

student.name="taoshihan";

student.age=30;

student.score=100;

printf("name=%s n",student.name);//c struct array

structstu{char *name;intage;floatscore;

} classes[5];structstu1{char *name;intage;floatscore;

} classes1[2]={

{"taoshihan",20,100.00},

{"lisi",20,90}

};structstu2{char *name;intage;floatscore;

} classes3[]={

{"taoshihan",20,100}

};

printf("%s n",classes1[1].name);//Traverse the array of structures

structpeople{"chaper5.c" 71L, 1199C 1,1Top//Traverse the array of structures

structpeople{char *name;intage;floatscore;

} d[]={

{"taoshihan",20,100},

{"lisi",30,90}

};int len=sizeof(d)/sizeof(d[0]);

printf("d length=%d n",len);for(int i=0;i

printf("loop...%s,%d,%.1f n",d[i].name,d[i].age,d[i].score);

}//C language structure and pointer

structperson1{char *name;intage;floatscore;

} a={"taoshihan",20,100},*b=&a;struct person1 *c=&a;//Get the structure member

printf("b.name=%s n",(*b).name);

printf("c.name=%s n",c->name);

}

内容来源于网络如有侵权请私信删除

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值