函数体中定义的结构体和类型
生活随笔
收集整理的這篇文章主要介紹了
函数体中定义的结构体和类型
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
源代碼:
1 #include <stdio.h> 2 struct smonth // point 1 3 { 4 int a; 5 int b; 6 }; 7 8 int func1() 9 { 10 struct smonth{ 11 int a; 12 int b; 13 }; 14 15 typedef int LOVE; // point 2 16 17 LOVE a = 5; 18 struct smonth s; 19 s.a = 6; 20 s.b = 7; 21 printf("func 1 : %d %d\n",s.a,s.b); 22 return 0; 23 } 24 25 int func2() 26 { 27 struct smonth{ 28 int c; 29 int d; 30 int f; 31 }; 32 33 // LOVE a = 5; // point 3 34 struct smonth s; 35 s.c = 8; 36 s.d = 9; 37 s.f = 10; 38 printf("func 2 : %d %d %d\n",s.c,s.d,s.f); 39 return 0; 40 } 41 42 int func3() 43 { 44 struct smonth s; 45 s.a = 11; 46 s.b = 12; 47 printf("func 3 : %d %d\n",s.a,s.b); 48 return 0; 49 } 50 51 int main() 52 { 53 func1(); 54 func2(); 55 func3(); 56 return 0; 57 }輸出結果:
zhangxu@Ivy-debian-64:~$ ./a.out func 1 : 6 7 func 2 : 8 9 10 func 3 : 11 12如果把point 1處的定義注釋掉:
zhangxu@Ivy-debian-64:~$ gcc test_struct.c test_struct.c: In function ‘func3’: test_struct.c:44:16: error: storage size of ‘s’ isn’t known如果把point 3處的注釋去掉:
zhangxu@Ivy-debian-64:~$ gcc test_struct.c test_struct.c: In function ‘func2’: test_struct.c:33:2: error: unknown type name ‘LOVE’由上面的實驗,容易發現,在函數體中定義的結構體和使用typedef 定義的變量類型的作用域都是此函數本身。
?
?
轉載于:https://www.cnblogs.com/godjesse/p/3328764.html
總結
以上是生活随笔為你收集整理的函数体中定义的结构体和类型的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: MC新手入门(三十)------ 逻辑运
- 下一篇: 虚拟视频驱动程序vivi.c源码分析