Search code examples
cc99object-type

What is the definition of Incomplete Type and Object Type in C?


What is the definition of Incomplete Type and Object Type in C? Also, could you provide some examples of each?

ANSI C99 mentions both type categories in various places, though I've found it difficult to understand what each of them means exactly (there is no paragraph/sentence explicitly defining what they are).


Solution

  • Let's go to the online C standard (draft n1256):

    6.2.5 Types

    1 The meaning of a value stored in an object or returned by a function is determined by the type of the expression used to access it. (An identifier declared to be an object is the simplest such expression; the type is specified in the declaration of the identifier.) Types are partitioned into object types (types that fully describe objects), function types (types that describe functions), and incomplete types (types that describe objects but lack information needed to determine their sizes).

    Examples of incomplete types:

    struct f;    // introduces struct f tag, but no struct definition
    int a[];     // introduces a as an array but with no defined size
    

    You cannot create instances of incomplete types, but you can create pointers and typedef names from incomplete types:

    struct f *foo;
    typedef struct f Ftype;
    

    To turn the incomplete struct type into an object type, we have to define the struct:

    struct f
    {
      int x;
      char *y;
    };