zoukankan      html  css  js  c++  java
  • 函数体中定义的结构体和类型

    源代码:

     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
    ",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
    ",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
    ",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 定义的变量类型的作用域都是此函数本身。

  • 相关阅读:
    constraint更新表列约束默认值
    sql语句 关于日期时间、类型转换的东西
    SQL数据库完全复制
    SQLServer语句 汇总
    SQL Server Profiler使用方法
    SQL语句-批量插入表(表数据插表)
    VS 快捷键
    外部引用 jQuery 库
    mongodb笔记
    Ubuntu16.04安装live-server
  • 原文地址:https://www.cnblogs.com/godjesse/p/3328764.html
Copyright © 2011-2022 走看看