设为首页 加入收藏

TOP

C语言的作用域/namespace分析
2014-11-23 20:29:13 】 浏览:9892
Tags:语言 作用 /namespace分析

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


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


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


  好吧,我们从代码开始:


  原始代码


  Code


  1 #include


  2 #include


  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 }


】【打印繁体】【投稿】【收藏】 【推荐】【举报】【评论】 【关闭】 【返回顶部
上一篇如何在vc6.0中运行c语言程序 下一篇C语言中实现通用双链表

最新文章

热门文章

Hot 文章

Python

C 语言

C++基础

大数据基础

linux编程基础

C/C++面试题目