Thread: Namespaces, and Enums and Scope

  1. #1
    Registered User
    Join Date
    Apr 2015
    Posts
    10

    Namespaces, and Enums and Scope

    Hello!

    I have two questions. I need help understanding what a namespace is in C; and the scope of enum constants is puzzling me.

    #1 The C11 standard draft states,
    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 the member via the . or -> operator);
    — all other identifiers, called ordinary identifiers (declared in ordinary declarators or as enumeration constants).

    32) There is only one name space for tags even though three are possible.
    What exactly is a namespace in C?
    Take a look at this code:
    Code:
    int main(void) {
        {
            struct my_struct { int i; };
            int i, j;
        }
    
        {
            struct my_struct { char *s; };
        }
    
        return 0;
    }
    Although both are structure tags, is it correct to say that these two identical tags are in different namespaces?

    So in the first block: my_struct is in one namespace; the two local variables in another; and the (only) member of my_struct in a third namespace; but the two structure tags are not in the same namespace, otherwise an error would occur.


    #2 Why does the following compile?
    Code:
    #include <stdio.h>
    
    
    int main(void) {
        struct struct_t {
            enum enum_t { A, B, C, D } my_enum;
            int i;
            double d;
        } my_struct;
    
    
        enum enum_t var = B;    // HELP
    
    
        printf("%d\n", D);        // HELP
        printf("%d\n", var);
        return 0;
    }
    Before I compiled the code, I assumed that the enum constants were only known inside the struct, but it turned out that was not the case. Also, how can enum_t be accessible outside of struct_t?

    How does it work?!

    Thank you for help.

  2. #2
    Registered User
    Join Date
    May 2010
    Posts
    4,632
    Although both are structure tags, is it correct to say that these two identical tags are in different namespaces?
    No, they are in different scopes, because of the use of the braces.

    The following illustrates the third point in your quote:

    Code:
    struct myStruct1
    {
       int a;
       int b;
    };
    
    struct myStruct2
    {
       int b;
       int c;
    };
    In the above snippet the structure member variables are considered in the scope of the structure. So myStruct1.b is different from myStruct2.b even though they have the same names.

    Jim

  3. #3
    Registered User
    Join Date
    Jun 2015
    Posts
    1,640
    Namespaces are different from scopes. The following are all in the same scope but are (for the most part) in different namespaces.
    Code:
    {
        struct a {      // struct,union,or enum tag
            int a;      // struct or union members (each struct/union has it's own namespace for members)
        };
    
        int a;          // ordinary identifier
    
    a:  ;               // label name
    
        enum e {        // struct, union, or enum tag
                           // (can't be a as that would conflict with struct a)
            f, g, h     // ordinary identifier (enum constants)
                           // (can't be a as that would conflict with int a)
        };
    }
    Although both are structure tags, is it correct to say that these two identical tags are in different namespaces?
    No. They are in the same namespace but since they are in different scopes they don't conflict.

    how can enum_t be accessible outside of struct?
    The enum constants are all dumped into the current scope. The braces around struct members do not constitute a scope. So in your example the constants are dumped into main's top-level scope.
    Last edited by algorism; 07-10-2015 at 10:01 AM.

  4. #4
    Registered User
    Join Date
    Apr 2015
    Posts
    10
    Hi; so two identifiers may belong to the same namespace and have different scopes (whether nested or not).

  5. #5
    Registered User
    Join Date
    Apr 2015
    Posts
    10
    Also, I found this:
    Unlike C++, C has no struct scope: names declared within a struct/union/enum declaration are in the same scope as the struct declaration (except that data members are in their own member name space)
    src=http://en.cppreference.com/w/c/language/scope

  6. #6
    Registered User
    Join Date
    Jun 2015
    Posts
    1,640
    Quote Originally Posted by atran View Post
    Hi; so two identifiers may belong to the same namespace and have different scopes (whether nested or not).
    Absolutely. I added a little to the end of my previous answer.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 3
    Last Post: 01-04-2014, 11:40 AM
  2. namespaces
    By drrcknlsn in forum C++ Programming
    Replies: 2
    Last Post: 01-24-2008, 03:47 PM
  3. Help with namespaces
    By slippy in forum C++ Programming
    Replies: 1
    Last Post: 01-12-2008, 09:36 PM
  4. Namespaces
    By the pooper in forum C Programming
    Replies: 8
    Last Post: 01-21-2005, 09:06 AM
  5. namespaces
    By Mario in forum C++ Programming
    Replies: 3
    Last Post: 05-27-2002, 02:57 PM