当前位置:首页 > 通信资讯 > 正文

c++中struct的用法(c需要struct)

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

?
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 #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()函数之前,在之前的话,整个程序都可以调用,这也是最常用的方式;而在内部的话,只能在函数内使用,也就是说在声明的函数内可以使用,类似于局部变量的概念。如下

?
1 2 3 4 5 6 7 8 9 10 11 12 13 14 int main(void) { struct test{ int num; }; test hello;//right } void t() { test t; //wrong }
如果您对该产品感兴趣,请填写办理(客服微信:xiaoxiongyidong)

为您推荐:

发表评论

◎欢迎参与讨论,请在这里发表您的看法、交流您的观点。