Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Incomplete types in C

6.2.5

At various points within a translation unit an object type may be incomplete (lacking sufficient information to determine the size of objects of that type).

Also

6.2.5 19) The void type comprises an empty set of values; it is an incomplete object type that cannot be completed.

And

6.5.3.4 The sizeof operator shall not be applied to an expression that has function type or an incomplete type,

But Visual Studio 2010 prints 0 for

printf("Size of void is %d\n",sizeof(void));

My question is 'What are incomplete types'?

struct temp
{
    int i;
    char ch;
    int j;
};

Is temp is incomplete here? If yes why it is incomplete(We know the size of temp)? Not getting clear idea of incomplete types. Any code snippet which explains this will be helpful.

like image 257
StackIT Avatar asked Dec 21 '22 00:12

StackIT


1 Answers

Your struct temp is incomplete right up until the point where the closing brace occurs:

struct temp
{
    int i;
    char ch;
    int j;
};// <-- here

The structure is declared (comes into existence) following the temp symbol but it's incomplete until the actual definition is finished. That's why you can have things like:

struct temp
{
    int i;
    char ch;
    struct temp *next; // can use pointers to incomplete types.
};

without getting syntax errors.

C makes a distinction between declaration (declaring that something exists) and definition (actually defining what it is).

Another incomplete type (declared but not yet defined) is:

struct temp;

This case is often used to provide opaque types in C where the type exists (so you can declare a pointer to it) but is not defined (so you can't figure out what's in there). The definition is usually limited to the code implementing it while the header used by clients has only the incomplete declaration.

like image 170
paxdiablo Avatar answered Dec 24 '22 02:12

paxdiablo