static struct、typedef struct

今天看到这样的写法,有点蒙,如下

static struct
{
	char a;
	char b;
} hrpsCb;

测试程序如下

#include <stdio.h>
static struct
{
	char a;
	char b;
} hrpsCb;
int main()
{
    hrpsCb.a = 1;
	hrpsCb.b = 2;
   printf("Hello, World! %d %d\n",hrpsCb.a,hrpsCb.b);
   
   return 0;
}

结果如下
在这里插入图片描述

其实上述写法是在构造结构体类型同时声明了一个结构体变量,是占用内存的,并修饰为static

注意要和如下typedef区分

typedef struct
{
	char c;
	char d;
} hrpsConn_t;

这是用typedef给此结构体类型取别名,本质还是构造结构体,不占内存,要做好区分。

struct总结如下

构造结构体类型,不占内存

struct 结构体名{
	结构体成员
}struct stu{
    char *name;  //姓名
    int num;  //学号
    int age;  //年龄
    char group;  //所在学习小组
    float score;  //成绩
};

用这个类型定义stu1, stu2变量,占用内存

struct stu stu1, stu2;

上面2步可以合在一起,定义结构体的同时定义结构体变量

struct stu{
    char *name;  //姓名
    int num;  //学号
    int age;  //年龄
    char group;  //所在学习小组
    float score;  //成绩
} stu1, stu2;

如果后面不在需要定义其他struct stu类型变量,则结构体名可以省略,如下

struct{  //没有写 stu
    char *name;  //姓名
    int num;  //学号
    int age;  //年龄
    char group;  //所在学习小组
    float score;  //成绩
} stu1, stu2;

,只不过例子用static修饰

  • 4
    点赞
  • 15
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值