关于struct 与 typedef struct

在以前学c语言时候用struct时是这样的:

#include <stdio.h>
#include <stdlib.h>

struct node{
    int a;
};

int main()
{
    node b;
    return 0;
}

c语言规定要用struct node b;来声明一个对象,或者如下:

#include <stdio.h>
#include <stdlib.h>

typedef struct node{
    int a;
}node_;

int main()
{
    node_ b;
    return 0;
}

此时node就相当于区别不同结构体的一个标识符没有实际意义,当然c语言中如下写也行:

#include <stdio.h>
#include <stdlib.h>

typedef struct{
    int a;
}node_;

int main()
{
    node_ b;
    return 0;
}

而在c++里面想怎么定义都行如下代码:

#include <iostream>
#include <stdio.h>
#include <stdlib.h>

using namespace std;

struct node{
    int a;
};

typedef struct{
    int a;
}p;

typedef struct f{
    int a;
};

typedef struct x{
    int a;
}g;
int main()
{
    node b;
    p c;
    f u;
    g s;
    x l;
    return 0;
}

当然我试这么多c++写法的原因只是想说c++更方便了,为了保证代码可读性最好还是按照typedef struct node  或者  struct node这种写法


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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值