【構造体ポインタ】

構造体ポインタは構造体のアドレスであり、構造体変数はメモリに格納され、開始アドレスも持ちます。
このアドレスを格納する変数を定義すると、この変数は構造体ポインター変数になります。

構造体ポインタ変数の定義方法:

struct 结构体类型名 * 结构体指针变量名;

構造体ポインター変数のメンバーへの参照

(*结构体指针变量名).成员
结构体指针变量名‐>成员
#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;
}

結果:
ここに画像の説明を挿入

おすすめ

転載: blog.csdn.net/shuting7/article/details/130351928