在C,有类型的两个不同的命名空间:结构/联合/枚举标记名称的命名空间和typedef名称的命名空间。
name.c
$ cat name.c
#include<stdio.h>
typedef long long long2;
int long2() {
return 4;
}
int main() {
printf("hello, world!");
return 0;
}
$ gcc name.c -o name
name.c:4: error: 'long2' redeclared as different kind of symbol
name.c:3: error: previous declaration of 'long2' was here
$
name2.c
$ cat name2.c
#include<stdio.h>
int four() {
return 4;
}
struct dummy {
int member;
};
int main() {
struct dummy four;
}
$ gcc name2.c -o name2
$
我想了解Ç命名空间冲突。
在第一种情况下,为什么会出现冲突?函数是否也属于typedef命名空间?
在第二种情况下,为什么没有冲突呢?函数和变量都被命名为四。为什么编译器允许这样做?
&four
应该如何解决?
结构/联合/枚举是否共享一个名称空间?也就是说'struct T'和'union T'能够很好的结合在一起吗? – iBug 2017-11-17 10:00:03