c语言的typedef struct 对应java参数类型,C语言:struct和typedef

1、没有设置结构体名,相当于一个匿名结构体,没有结构体名,后面就没法用该结构体定义新的变量。

#include

#include

struct{

int x;

int y;

}Test;

int main()

{

Test.x = 100;

printf("%d",Test.x);

}

2、设置了结构体名a,就可以利用struct a Test2, 来构造一个新的结构体变量Test2,相比上一个的优点是可以直接在main函数里继续构造新的结构体变量(因为有结构体名字了)

#include

#include

struct A{

int x;

int y;

}Test1;

int main()

{

struct A Test2;

Test1.x = 50;

Test2.x = 100;

printf("%d,%d",Test1.x,Test2.x);

}

3、使用 typedef的结构体,其实末尾这里的Test1,Test2不是结构体的变量,而都是struct B的别名,也就是说使用 typedef的时候没有默认的结构体变量,需要构造新的结构体变量的话,必须通过main函数里的 Test1 test1 或者 Test2 test2来构造一个结构体变量 test1或者test2,相比2来说,拥有typedef的结构体少了一个能够设置初始的结构体变量的地方,但是在main函数中可以方便的少写 struct这个关键字

#include

#include

typedef struct B{

int x;

int y;

}Test1,Test2;

int main()

{

Test1 test1;

Test2 test2;

test1.x = 50;

test2.x = 100;

printf("%d",test1.x);

}

4、这里相当于结构体没有名字,是一个匿名的结构体,此时末尾的Test也是一个别名而已,只不过是匿名结构体的别名。

#include

#include

typedef struct{

int x;

int y;

}Test;

int main()

{

Test test;

test.x = 100;

printf("%d",test.x);

}

总结:

有typedef的时候,末尾Test的这个位置就变成了结构体的别名,就是等价struct A。

没有typedef的时候,末尾Test的这个位置就是一个结构体变量。struct后面有名字就方便后续增加结构体变量,没有名字就是一个匿名的结构体,后续不能增加结构体变量。

本文地址:https://blog.csdn.net/YiXiao1997/article/details/107301142

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值