typedef a struct before it's declared

前端 未结 7 1888
死守一世寂寞
死守一世寂寞 2020-12-09 15:14

I\'m not a beginner, I\'m very familiar with the following idiom:

typedef struct Foo_ Foo;// I know typedef struct Foo Foo is fine, I\'m just trying to make          


        
相关标签:
7条回答
  • 2020-12-09 15:33

    typedef is used to create an alias for a type. But that type doesn't necessarily exist when typedef'ed.

    For example,

    if you just do:

    struct Foo;
    

    and you never define the struct Foo anywhere in the program, then it'll still compile. Compiler would assume it's defined somewhere and continue. Only if you use it without defining the struct, an error will occur.

    It's the similar case with typedef as well.

    0 讨论(0)
  • 2020-12-09 15:37

    Under certain circumstances, it is valid to use a struct ... type before it is declared. It is a so-called "incomplete type" then.

    For example, it is valid to declare a variable as a pointer to an "incomplete" struct, as well as (as you can see) a typedef.

    0 讨论(0)
  • 2020-12-09 15:38

    This is completely fine. The first use of struct tag like yours is a forward declaration of the struct type.

    Beware though that your usage of _Foo is not conforming. Identifiers with leading underscore and following capital letter are reserved. Don't do that. Trailing underscore would be ok.

    0 讨论(0)
  • 2020-12-09 15:39

    A typedef declaration lets you define your own identifiers that can be used in place of type specifiers such as int, float, and double. A typedef declaration does not reserve storage.

    for more info http://publib.boulder.ibm.com/infocenter/macxhelp/v6v81/index.jsp?topic=%2Fcom.ibm.vacpp6m.doc%2Flanguage%2Fref%2Fclrc03typdef.htm

    0 讨论(0)
  • 2020-12-09 15:42
    ISO c99 : 6.2.1 Scopes of identifiers
    

    7

    Structure, union, and enumeration tags have scope that begins just after the appearance of the tag in a type specifier that declares the tag.

    typedef struct _Foo Foo; // You can do this because it's just the typedef the new type
    
    struct _Foo *myfoo ; // It's pointer to struct _Foo (an incomplete type)
                          //but make sure before using myfoo->value   
                        // struct definition should be available
    
    struct _Foo MyFoo;  // It's  definition of MyFoo but don't forget 
                        // to give the definition of struct _Foo (gcc extension). 
    
    struct _Foo;  // forward declaration
    
    struct _Foo    // It's the definition
    {
        int value;
        Foo *link;
    };
    

    Simply as for functions we do forward declaration or typedef before actual definition of function , So we can do it with struct also.

    void func(int );
    typedef void (*func_t)(int);
    
    void func(int x)
    {
     //actual definition
    }
    
    0 讨论(0)
  • 2020-12-09 15:53

    This is covered in 6.7.2.3p8:

    6.7.2.3 Tags

    Semantics
    [...]

    8 - If a type specifier of the form struct-or-union identifier occurs other than as [a struct-or-union definition] or [a struct-or-union declaration], and no other declaration of the identifier as a tag is visible, then it declares an incomplete structure or union type, and declares the identifier as the tag of that type.

    The type specifier struct Foo in typedef struct Foo Foo is not in a definition (struct Foo {...};) or a declaration (struct Foo;) so it falls under 6.7.2.3p8.

    Note that there's nothing special about a typedef; you could also e.g. write

    struct A { struct Foo *p; };
    

    and a previous definition or declaration is not required to be visible.

    However, in a function declaration or definition:

    void foo(struct Foo *p);
    

    if struct Foo is not previously declared then the scope of the declaration will just be the function declaration or definition, and it will not be type-compatible with any subsequent declaration or definition of Foo.

    0 讨论(0)
提交回复
热议问题