structure
A structure is a commonly used code in the coding process, which is convenient for collecting different types of member variables
declaration of the structure
struct tag
{
member-list;
}variable-list;
e.g. describe a person
typedef struct pepole
{
char name[20]//名字
int age;//年龄
char sex[5];//性别
char id[20];//身份证号
}Stu;
Types of structure members Structure
members can be scalars, arrays, pointers, or even other structures
Definition and initialization of structure variables
struct point
{
int x;
int y;
}p1;//声明类型的同时赋值
struct point p2;//定义结构体变量p2
struct Stu//类型声明
{
char name[15];//名字
int age;//年龄
};
struct Stu s={
"doudou",20};//初始化
struct Node
{
int data;
struct point p;
struct Node* next;
}n1={
10,{
4,5},NULL}; //结构体嵌套初始化
struct Node n2={
20,{
5,6},NULL};
struct Node n2={
20,{
5,6},NULL}; //结构体嵌套初始化
Access to structure members
Struct variable access members are accessed through the dot operator (.). The dot operator takes two operands
struct Stu
{
char name[20];
int age;
};
void print(struct Stu*ps)
{
printf("name=% age=%d\n",(*ps).name,(*ps).age);//使用结构体的指针访问指向对象的成员
printf("name=% age=%d\n",ps->name,ps->age);
}
int main()
{
struct Stu s={
"zhangsan",20};
print(&s);
return 0;
}