c – 使用typedef作为变量名,不会产生任何错误
考虑到这种数据结构:
typedef struct { float x; float y; } point; 我正在使用此功能来置换坐标: point permute(point M) { point N; N.x = M.y; N.y = M.x; return N; } 为什么声明一个名为(point)的变量,这是一个没有给出任何错误的typedef? int main(void) { point A; point B; int point = 7; A.x = 0; A.y = 1; B = permute(A); printf("A (%.2f,%.2f)n",A.x,A.y); printf("B (%.2f,B.x,B.y); printf("point = %dn",point); return 0; } 输出:http://ideone.com/wCxbjD A (0.00,1.00) B (1.00,0.00) point = 7 解决方法
范围.
在外部作用域中声明的标识符(在您的情况下,在文件作用域,即在任何函数之外)可以在内部作用域中重新声明(在您的情况下,在主函数内).内部声明隐藏外部声明,直到到达内部范围的末尾. 这通常适用于声明,而不仅仅是typedef名称. 一个简单的例子: #include <stdio.h> typedef struct { int x,y; } point; int main(void) { point p = { 10,20 }; /* p is of type point */ int point = 42; /* This hides the typedef; the name "point" now refers to the variable */ printf("p = (%d,%d),point = %dn",p.x,p.y,point); /* p is still visible here; its type name "point" is not because it's hidden. */ } 输出是: p = (10,20),point = 42 如果我们修改了上面的程序,将typedef移动到与变量声明相同的范围内: #include <stdio.h> int main(void) { typedef struct { int x,y; } point; point p = { 10,20 }; int point = 42; printf("p = (%d,point); } 我们得到一个错误; gcc说: c.c: In function ‘main’: c.c:5:9: error: ‘point’ redeclared as different kind of symbol c.c:3:34: note: previous declaration of ‘point’ was here (可能已经定义了C语言允许这样做,第二个声明点隐藏了第一个用于范围的其余部分,但设计者显然认为隐藏来自外部作用域的声明可能很有用,但在单个作用域内这样做会造成更多的混乱而不是它的价值.) 一个稍微复杂的例子: #include <stdio.h> int main(void) { typedef struct { int x,y; } point; { /* Create an inner scope */ point p = { 10,20 }; /* Now the type name is hidden */ int point = 42; printf("p = (%d,point); } /* We're outside the scope of `int point`,so the type name is visible again */ point p2 = { 30,40 }; printf("p2 = (%d,%d)n",p2.x,p2.y); } 这种隐藏可能不是最好的主意;对于两个不同的东西使用相同的名称,虽然编译器没有问题,但可能会让人类读者感到困惑.但是它允许您在块范围内使用名称,而不必担心可能已经包含在您所包含的所有头文件中的所有名称. (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |