1.C++的結構體變量在聲明的時候可以省略struct,在c中這樣是不可以的,例子如下
1 #include<iostream>
2 #include<string>
3 using namespace std;
4
5 struct test{
6 int num;
7 string name;
8 };
9
10 int main(void)
11 {
12 test t;
13 t.num=1;
14 t.name="jack";
15 cout<<t.num<<" "<<t.name<<endl;
16 }
2.c++的結構體聲明可以聲明在main()函數中,也可以在main()函數之前,在之前的話,整個程序都可以調用,這也是最常用的方式;而在內部的話,只能在函數內使用,也就是說在聲明的函數內可以使用,類似於局部變量的概念。如下
1 int main(void)
2 {
3 struct test{
4 int num;
5 };
6
7 test hello;//right
8
9 }
10
11 void t()
12 {
13 test t; //wrong
14 }