C语言的作用域/namespace分析
来源:优易学  2010-1-14 10:55:25   【优易学:中国教育考试门户网】   资料下载   IT书店

 

  [5] 所以代码中label x与其他命名不冲突

  Code

  1 int x(const int int_a) {return int_a;}

  2

  3 struct x

  4 {

  5     int x;

  6 };

  7

  8 #define x(x)  x

  9

  10 int main(int argc, char *argv[])

  11 {

  12     int *x = calloc(1, sizeof x);

  13

  14     x: (((struct x *)x)->x) = x(5);         //这里的label x存在于独立的namespace,与其他不冲突.

  15

  16     printf("%p\n", ((struct x *)x)->x);

  17

  18     return 0;

  19 }

  [6] 现在我们把代码中无效代码去掉,并把宏定义语句手动替换掉,是的代码简洁点

  Code

  1 struct x

  2 {

  3     int x;

  4 };

  5

  6 int main(int argc, char *argv[])

  7 {

  8     int *x = calloc(1, sizeof(int *));

  9

  10     //到此我们有自定义类型struct x和变量(int *)x,其中struct x作用域为全局,(int *)x作用域为main()

  11

  12     (((struct x *)x)->x) = 5;

  13     //            ↑

  14     //           这里的x是由(int *)强制转化成(struct x *),所以后面实际是给struct中的(int)x赋值

  15

  16     printf("%p\n", ((struct x *)x)->x);   //这里还是需要强制转化成struct,这样才能识别,然后得到(int)x的值

  17

  18     return 0;

  19 }

  到此代码中所有的x都说明了,这里再次总结下.

  Code

  1 #include <stdio.h>

  2 #include <stdlib.h>

  3

  4 int x(const int int_a) {return int_a;}      //全局的函数名

  5

  6 struct x                                    //全局的struct名,属于自定义类型名,所以不会跟上面的(int *(const int))x及下面main中的(int *)x冲突

  7 {

  8     int x;                                  //属于struct x的int型x

  9 };

  10

  11 #define x(x)  x                             //宏定义,会在预编译时进行代码扩展,所以并不会在编译时产生命名冲突

  12

  13 int main(int argc, char *argv[])

  14 {

  15     int *x = calloc(1, sizeof x);           //作用域为main的(int *)x; sizeof计算的是(int *)x大小.

  16

  17     x: (((struct x *)x)->x) = x(5);         //作为label的x独立存在于一个namespace.

  18

  19     printf("%p\n", ((struct x *)x)->x);

  20

  21     return 0;

  22 }

上一页  [1] [2] [3] [4] 

责任编辑:cyth

文章搜索:
 相关文章
热点资讯
资讯快报
热门课程培训