[编程] C语言的结构体
2018-06-18 03:56:45来源:未知 阅读 ()
结构体
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<len;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 <stdio.h> int main(){ printf("hello world"); //Structure variables 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 array struct stu{ char *name; int age; float score; } classes[5]; struct stu1{ char *name; int age; float score; } classes1[2]={ {"taoshihan",20,100.00}, {"lisi",20,90} }; struct stu2{ char *name; int age; float score; } classes3[]={ {"taoshihan",20,100} }; printf("%s \n",classes1[1].name); //Traverse the array of structures struct people{ "chaper5.c" 71L, 1199C 1,1 Top //Traverse the array of structures 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<len;i++){ printf("loop...%s,%d,%.1f \n",d[i].name,d[i].age,d[i].score); } //C language structure and pointer struct person1{ char *name; int age; float score; } 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); }
标签:
版权申明:本站文章部分自网络,如有侵权,请联系:west999com@outlook.com
特别注意:本站所有转载文章言论不代表本站观点,本站所提供的摄影照片,插画,设计作品,如需使用,请与原作者联系,版权归原作者所有
上一篇:[编程] C语言的二级指针
下一篇:最简分式
- 关于各种不同开发语言之间数据加密方法(DES,RSA等)的互通的 2020-06-07
- C语言程序结构 2020-05-31
- 数据结构—链表 2020-05-29
- C++17结构化绑定 2020-05-15
- 图 2020-05-02
IDC资讯: 主机资讯 注册资讯 托管资讯 vps资讯 网站建设
网站运营: 建站经验 策划盈利 搜索优化 网站推广 免费资源
网络编程: Asp.Net编程 Asp编程 Php编程 Xml编程 Access Mssql Mysql 其它
服务器技术: Web服务器 Ftp服务器 Mail服务器 Dns服务器 安全防护
软件技巧: 其它软件 Word Excel Powerpoint Ghost Vista QQ空间 QQ FlashGet 迅雷
网页制作: FrontPages Dreamweaver Javascript css photoshop fireworks Flash