tags:

views:

42

answers:

2

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).

+1  A: 

The types I know of are:

  • Incomplete type
  • object type
  • function type

Here's an example (also on codepad: http://codepad.org/bzovTRmz )

#include <stddef.h>

int main(void) {
    int i;
    struct incomplete *p1;
    int *p2;
    int (*p3)(void);

    p1 = NULL; /* p1 is a pointer to a incomplete type */
    p2 = &i;   /* p2 is a pointer to an object */
    p3 = main; /* p3 is a pointer to a function */

    return 0;
}

The struct incomplete can be defined (with a definite size) in another translation unit. This translation unit only needs the pointer though

pmg
Thanks for the answer pmg!
Dave Gallagher
+1  A: 

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;
};
John Bode
Thanks John, very clear answer! :)
Dave Gallagher