struct和typedef struct区别详解
来源:互联网 发布:自动化设备控制软件 编辑:程序博客网 时间:2024/05/21 14:00
struct和typedef struct区别详解
分两部分来讲述:
1 首先://注意在C和C++里不同
在C中定义一个结构体类型要用typedef:
typedef struct Student
{
int a;
}Stu;
于是在声明变量的时候就可:Stu stu1;(如果没有typedef就必须用struct Student stu1;来声明)
这里的Stu实际上就是struct Student的别名。Stu==struct Student
另外这里也可以不写Student(于是也不能struct Student stu1;了,必须是Stu stu1;)
typedef struct
{
int a;
}Stu;
但在c++里很简单,直接
struct Student
{
int a;
};
于是就定义了结构体类型Student,声明变量时直接Student stu2;
======================================================================================
2.其次:
在c++中如果用typedef的话,又会造成区别:
struct Student
{
int a;
}stu1;//stu1是一个变量
typedef struct Student2
{
int a;
}stu2;//stu2是一个结构体类型=struct Student
使用时可以直接访问stu1.a
但是stu2则必须先 stu2 s2;
然后 s2.a=10;
======================================================================================
分两部分来讲述:
1 首先://注意在C和C++里不同
在C中定义一个结构体类型要用typedef:
typedef struct Student
{
int a;
}Stu;
于是在声明变量的时候就可:Stu stu1;(如果没有typedef就必须用struct Student stu1;来声明)
这里的Stu实际上就是struct Student的别名。Stu==struct Student
另外这里也可以不写Student(于是也不能struct Student stu1;了,必须是Stu stu1;)
typedef struct
{
int a;
}Stu;
但在c++里很简单,直接
struct Student
{
int a;
};
于是就定义了结构体类型Student,声明变量时直接Student stu2;
======================================================================================
2.其次:
在c++中如果用typedef的话,又会造成区别:
struct Student
{
int a;
}stu1;//stu1是一个变量
typedef struct Student2
{
int a;
}stu2;//stu2是一个结构体类型=struct Student
使用时可以直接访问stu1.a
但是stu2则必须先 stu2 s2;
然后 s2.a=10;
======================================================================================
0 0
- struct和typedef struct区别详解
- typedef struct 和 struct区别
- struct和typedef struct区别
- struct和typedef struct区别
- struct和typedef struct区别
- struct和typedef struct 区别
- 【typedef struct 和 struct 区别】
- typedef struct和struct区别
- struct 和 typedef struct 区别
- struct和typedef struct区别
- struct和typedef struct区别
- struct和typedef struct区别
- typedef struct和struct区别
- typedef struct 和struct区别
- typedef struct和struct区别
- struct和typedef struct详解
- struct/typedef struct 详解
- typedef struct和struct的区别
- MFC中隐藏、显示菜单栏
- light oj 1236 【大数分解】
- 18大经典数据挖掘算法小结
- Ubuntu server 14.04 配置Memcache,以及Memcache的简单使用
- 快速排幂-----Digit Rightmost
- struct和typedef struct区别详解
- csdn 黑白球
- The C10K problem翻译
- 等高布局
- python面试题大全
- 获取CPU序列号、网卡MAC地址、硬盘序列号
- 浅谈C++多态性
- [JSOI2007] 奶酪 题解 贪心 O(1)
- 线程条件变量pthread_cond_t