结构体
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);
}