2012-09-25 4 views
3

C에서 유형이 정의 된 다른 네임 스페이스에 대해 SO를 읽었습니다. Structs와 Unions의 네임 스페이스와 typedef의 네임 스페이스가 있습니다.C의 유형 이름 공간

네임 스페이스가 정확한 이름입니까? C에 몇 개의 네임 스페이스가 있습니까?

답변

5

식별자의 http://www.open-std.org/jtc1/sc22/wg14/www/docs/n1570.pdf

6.2.3 이름 공간으로부터 6.2.3

참조

If more than one declaration of a particular identifier is visible at 
any point in a translation unit, the syntactic context disambiguates uses 
that refer to different entities. 

Thus, there are separate name spaces for various categories of identifiers, 
as follows: 
— label names (disambiguated by the syntax of the label declaration and use); 

— the tags of structures, unions, and enumerations (disambiguated by 
following any32) of the keywords struct, union, or enum); 

— the members of structures or unions; each structure or union has a 
separate name space for its members (disambiguated by the type of the 
expression used to access themember via the . or -> operator); 

— all other identifiers, called ordinary identifiers (declared in ordinary 
    declarators or as enumeration constants). 
2

여기에 "네임 스페이스"가 올바른 단어인지 확실하지 않지만 귀하가 의미하는 바를 알고 있습니다. 그들이 서로 충돌하지 않는

당신은 (나는 지금 그 단어를하겠습니다)

union name1 { int i; char c; }; 
struct name2 { int i; char c; }; 
enum name3 { A, B, C }; 
typedef int name4; 
int name5; 

여기 name1, name2name3는 별개 "네임 스페이스"에 할 수 있습니다.

그들을 사용하는 것은 각각의 키워드로 사용을 접두사로 필요하다는 의미 :

struct name1 var; // valid 
name1 var; // invalid 
한편

, name4name5 글로벌 "네임 스페이스"의 라이브와 충돌합니다. 따라서 typedef int name4;을 가진 후에는 그 이름이 name4 인 변수를 정의 할 수 없습니다.

BTW : 레이블도 고유 한 네임 스페이스를 정의합니다.