函数体中定义的结构体和类型

源代码:

 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

  • 2
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值