c与c++中struct的区别

c语言中,不能直接用结构体名来声明变量。

c++中,可以直接用结构体名来声明变量。

//c语言//
//声明 
struct stu
{
    ...
};
//定义 
struct stu student;
//c++//
//声明
struct stu
{
    ...
};
//定义
1.struct stu student;
2.stu student;

如果想在c语言中直接用结构体名定义变量,需要用到 typedef 

//typedef的一般用法
typedef
type new_type;

特别的当type为用户自定义类型时,type 和 new_type 可以相同。

用于结构体时 

typedef struct stu
{
    ...
}Stu;
//定义
1.Stu student;
2.struct stu student;

 

posted @ 2019-05-23 11:31  jawide  阅读(4050)  评论(0)    收藏  举报