c语言结构体编程,[编程] C语言的结构体详解
結(jié)構(gòu)體
struct 結(jié)構(gòu)體名{}變量名;
結(jié)構(gòu)體變量:
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語言結(jié)構(gòu)體數(shù)組
struct stu{
char *name;
int age;
float score;
} classes[5];
遍歷結(jié)構(gòu)體數(shù)組
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語言結(jié)構(gòu)體和指針
struct 結(jié)構(gòu)體名*變量名;
struct person1{
char *name;
int age;
float score;
} a={"taoshihan",20,100},*b=&a;
struct person1 *c=&a;
獲取結(jié)構(gòu)體成員
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);
}
總結(jié)
以上是生活随笔為你收集整理的c语言结构体编程,[编程] C语言的结构体详解的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 怎么把文本文档txt改成html,如何将
- 下一篇: c语言编辑输出后汉字乱码,为什么这个程序