了解Ç命名空间 [英] understanding C namespaces

查看:126
本文介绍了了解Ç命名空间的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

从<一个报价href=\"http://stackoverflow.com/questions/612328/difference-between-struct-and-typedef-struct-in-c/612350#612350\">here,

在C,有类型的两种不同的命名空间:结构/联合/枚举标记名称的命名空间和typedef名称的命名空间

In C, there are two different namespaces of types: a namespace of struct/union/enum tag names and a namespace of typedef names.

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
$ 

我想了解Ç命名空间的冲突。

I am trying to understand C namespace conflicts.


  • 在第一种情况下,为什么会出现冲突?千万功能也属于类型定义命名空间?

  • In the first case, why is there a conflict? Do functions also belong to the typedef namespace?

在第二种情况下,为什么没有冲突呢?功能和可变两个被命名为四人。为什么编译器允许?如何为&放大器;•四应该是解决

In the second case, why is there no conflict at all? The function and the variable both are named four. Why does the compiler allow that? How is &four supposed to be resolved?

推荐答案

C有四个不同的名字空间标识符:

C has four different name spaces for identifiers:


  • 标签的名称(转到键入)。

  • 标签(的结构,工会和枚举名称)。

  • 结构和联合的
  • 成员(这些都有每个结构/联合不同的命名空间)。

  • 所有其它标识符(函数名,对象名称,类型(DEF)名称,枚举常量,等等)。

另请参见C99 6.2.3。

See also C99 6.2.3.

所以你的两个问题都可以回答是:

So your two question can be answered as:


  1. 是,函数名和typedef名称共享相同的命名空间。

  2. 没有冲突,因为编译器将使用范围规则(函数或对象的名称)。在主要的标识符来表示的阴影的全局函数的名称,一些事情如果设置了警告级别足够高,你的编译器会发出警告。

  1. Yes, function names and typedef names share the same name space.
  2. No conflict, because the compiler will use scope rules (for function or object names). The identifier in main is said to shadow the global function name, something your compiler will warn you about if you set the warning levels high enough.

这篇关于了解Ç命名空间的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆