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为用户自定义类型时,typenew_type 可以相同。

用于结构体时

typedef struct stu
{
    ...
}Stu;
//定义
1.Stu student;
2.struct stu student;
  1. https://www.cnblogs.com/jawide/p/10910909.html
  2. https://www.cnblogs.com/zhougong/p/8876539.html

猜你喜欢

转载自blog.csdn.net/qq_36301365/article/details/93481599