加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

c++中struct使用注意事项

发布时间:2020-12-16 05:40:40 所属栏目:百科 来源:网络整理
导读:1.C++的结构体变量在声明的时候可以省略struct,在c中这样是不可以的,例子如下 #includeiostream#includestringusing namespace std;struct test{ int num; string name;};int main(void){ test t; t.num=1; t.name="jack"; coutt.num" "t.nameendl; } 2.c+

1.C++的结构体变量在声明的时候可以省略struct,在c中这样是不可以的,例子如下

#include<iostream>
#include<string>
using namespace std;

struct test{
  int num;
  string name;
};

int main(void)
{
  test t;
  t.num=1;
  t.name="jack";
  cout<<t.num<<" "<<t.name<<endl;    
}

2.c++的结构体声明可以声明在main()函数中,也可以在main()函数之前,在之前的话,整个程序都可以调用,这也是最常用的方式;而在内部的话,只能在函数内使用,也就是说在声明的函数内可以使用,类似于局部变量的概念。如下

int main(void)
{
  struct test{
    int num;
  };
  
  test hello;//right
  
}

void t()
{
  test t; //wrong
}

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读