数据结构 / 结构体指针

1. 格式

cpp 复制代码
     struct 结构体名
    {
        数据类型 成员1;
        数据类型 成员2;  
        .... 
    };
    struct 结构体名 *指针变量名

2. 结构体指针指向普通变量的地址

cpp 复制代码
     struct CAR
    {
        char name[10];
        int price;    
    };
    struct CAR car={"byd",160}; 
    struct CAR *p=&car; //p是指向结构体变量car的指针
    // p->name 等价于car.name
    // (*p).name //不常用
    // (&car)->name //不常用
 
    // p->price 等价于car.price
    //(*p).price //不常用
    //(&a)->price //不常用

3.结构体指针指向结构体数组的地址

cpp 复制代码
     struct CAR
    {
        char name[20];
        int price;    
    };

    struct CAR car[3]={"byd", 160,"wuling",50,"geely", 90};
    struct CAR *p=car; //数组指针
    for(int i=0;i<3;i++)
    {
        printf("%s  %d\n",(p+i)->name,(p+i)->price);    
    }

4.结构体指针指向堆区的内存

cpp 复制代码
      struct CAR
    {
        char name[10];
        int price;    
    };

    struct CAR car[4];
    struct CAR *p=(struct CAR*)malloc(sizeof(struct CAR)*4);
相关推荐
xianyinsuifeng几秒前
Oracle 10g → Oracle 19c 升级后问题解决方案(Pro*C 项目)
c语言·数据库·oracle
XMYX-05 分钟前
Linux 物理机如何区分 SSD 与 HDD ——以 DELL PERC H730 Mini 为例
linux·运维
轻松Ai享生活7 分钟前
5 天学习 Linux Kernel 主要原理 | Day 3:内存管理与Swap机制
linux
轻松Ai享生活8 分钟前
QAT 与 GPU 在SHA-1 运算中的优劣
linux
songx_9912 分钟前
leetcode9(跳跃游戏)
数据结构·算法·游戏
学c语言的枫子23 分钟前
数据结构——双向链表
c语言·数据结构·链表
行思理25 分钟前
linux 安全与防护,全方向讲解
linux·安全·github
tan180°1 小时前
Boost搜索引擎 查找并去重(3)
linux·c++·后端·搜索引擎
yongui478342 小时前
CentOS系统如何查看当前内存容量
linux·运维·centos
Boop_wu2 小时前
[数据结构] 栈 · Stack
数据结构