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

  在csdn上看到一段代码。觉得很有意思,于是便自己动动手分析分析。

  这是用于分析C语言中的作用的一段代码,值得研究研究。

  代码中calloc之后并没有free掉,这是个不好的习惯. :)

  好吧,我们从代码开始:

  原始代码

  Code

  1 #include <stdio.h>

  2 #include <stdlib.h>

  3

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

  5

  6 struct x

  7 {

  8     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);

  16

  17     x: (((struct x *)x)->x) = x(5);

  18

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

  20

  21     return 0;

  22 }

  23 /*

  24 output:

  25 0x5

  26 */

  [0] 变量名(包括指针名,函数名)和自定义类型名(struct)存在于不同namespace.所以b不会和a,c冲突

  Code

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

  2

  3 struct x                                    //b

  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);           //c

  13

  14     x: (((struct x *)x)->x) = x(5);

  15

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

  17

  18     return 0;

  19 }

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

责任编辑:cyth

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