The structure pointer is the address of the structure, and the structure variable is stored in the memory, and also has the starting address.
Define a variable to store this address, then this variable is a structure pointer variable.
The definition method of the structure pointer variable:
struct 结构体类型名 * 结构体指针变量名;
References to members of structure pointer variables
(*结构体指针变量名).成员
结构体指针变量名‐>成员
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
struct stu{
int id;
char name[32];
char sex;
int age;
};
int main(int argc, char *argv[])
{
//定义一个结构体指针变量
struct stu *s;
//在堆区开辟结构体空间并将其地址保存在结构体指针变量中
s = (struct stu *)malloc(sizeof(struct stu));
s->id = 1001;
strcpy(s->name, "Lila");
s->sex = 'F';
s->age = 30;
printf("%d - %s - %c - %d\n", s->id, s->name, s->sex, s->age);
return 0;
}
Results of the: