c语言结构体(嵌入式开发)

1.结构体

1).定义

用户自定义的数据类型,结构体中可以包含若干不同数据类型的成员变量,也可以数据类型相同,使这些数据组合起来反映一个信息。

2).格式

struct 结构体名 {

数据类型 成员变量名;

数据类型 成员变量名;

.

.

.

数据类型 成员变量名;

};

3).结构体变量

  1. 概念:通过结构体类型定义的变量

  2. 格式:

    1. 先定义结构体,再定义结构体变量

      struct 结构体名 变量名;

      struct student
      {
          int id;
          int age;
          float score;
          int number;
      };
      struct student stu1;
      
    2. 定义结构体的同时定义结构体变量

      struct student
      {
          int id;
          int age;
          float score;
          int number;
      }stu,stu1;
      
    3. 缺省结构体名定义变量

      struct
      {
          int id;
          int age;
          float score;
          int number;
      }stu1;
      

4).赋值

  1. 定义的同时用{}赋值

    struct student
    {
        int id;
        int age;
        float score;
        int number;
    };
    struct student stu1= {1,23,60,123456}
  2. 定义完变量,单独赋值

    struct student
    {
        int id;
        int age;
        float score;
        int number;
    };
    struct student stu1;
    stu1.id = 2;
    stu1.age = 23;
    
  3. 点等法赋值

    struct student
    {
        int id;
        int age;
        float score;
        int number;
    };
    struct student stu1={
    	.id = 2;
    	.age = 20;
    }
    

5).访问

通过 . 访问:变量名.成员变量

补充:typedef int size_t;

  1. 定义结构体同时重定义

    typedef struct student
    {
        int id;
        int age;
        float score;
        int number;
    }STU;
    STU stu1;
    
  2. 先定义结构体,再重定义

    struct student
    {
        int id;
        int age;
        float score;
        int number;
    };
    typedef struct student STU;
    

2.结构体数组

  1. 概念:具有相同结构体类型的变量组成结构体数组

  2. 定义的格式:

    1. 定义结构体的同时,定义数组

      struct student
      {
          int id;
          int age;
          float score;
          int number;
      }stu[5];
      
    2. 先定义结构体,再定义数组

      struct student
      {
          int id;
          int age;
          float score;
          int number;
      };
      struct student stu[5];
      
  3. 赋值

    1. 定义数组的同时赋值

      struct student
      {
          int id;
          int age;
          float score;
          int number;
      };
      struct student stu[5] = {
      {1,20,30.2},{2,22,40.6},{3,5,10},{4,56,32},{5,80,100}
      }
      
    2. 先定义,后赋值

      struct student
      {
          int id;
          int age;
          float score;
          int number;
      }stu[5];
      stu[0].id=1;
      stu[0].age = 100;
      stu[0].score = 0;
      
  4. 结构体数组大小

    sizeof(struct student)*元素个数

3.结构体指针

  1. 概念:指向结构体变量的指针

  2. 定义格式:
    struct 结构体名 *指针名;

    struct student
    {
        int id;
        int age;
        float score;
    }stu;
    struct student  *  p = &stu;
    
  3. 赋值

    格式:

    指针名->成员变量名

    p->id = 1;

  4. 结构体指针的大小
    sizeof( p )=4

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值