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

 

  [4] #define在预编译阶段替换其后面代码,所以对#define后面的代码来说,x(n)被替换为n,所以在编译时代码会扩展为如下:

  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) = 5;         //x(5)替换为5,所以并没有调用函数int x(const int)

  15

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

  17

  18     return 0;

  19 }

  关于标签label.

  标签,仅仅是一个符号,存在一个label专用的namespace,仅对goto可见,所以不会与变量或者常量冲突.

  在MSDN的goto条目中也有相关的描述:

  "The set of identifier names following a goto has its own name space so the names do not interfere with other identifiers. Labels cannot be redeclared."

  Code

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

  2 {

  3     int p = 5;

  4

  5     x: printf("line x.\n");

  6

  7     ++p;

  8

  9     if (p == 8) return 0;

  10

  11     goto x;

  12 }

  13

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

  15 {

  16     int x = 5;

  17

  18     x: printf("label x.\n");          //这里的label x与(int)x是无关的

  19

  20     ++x;

  21

  22     if (x == 8) return 0;

  23

  24     goto x;                          //goto会在标签namespace查找label x.

  25

  26     printf("x = %d | :( .\n", x); //无效语句

  27     return 0;                     //无效语句

  28 }

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

责任编辑:cyth

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