Tim
Tim

Reputation: 99616

Is there a non-array incomplete type?

All I have found in C11 Standard for incomplete types are incomplete array types.

I was wondering if there is a non-array incomplete type.

Upvotes: 4

Views: 269

Answers (4)

chux
chux

Reputation: 154602

For reference, what is incomplete, complete?

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) or complete (having sufficient information). C11 §6.2.5 1


In addition to potentially struct, union, arrays and always void, enum are temporarily incomplete in that their size is incomplete until the }

... The enumerated type is incomplete until immediately after the } that terminates the list of enumerator declarations, and complete thereafter. C11 §6.7.2.2 4

int main() {
  enum ee1 { a1 = 0, b1 = sizeof(int), c1 };
  printf("%zu\n", sizeof(enum ee1));  // OK

  // error: invalid application of 'sizeof' to incomplete type 'enum ee2'
  //                                        v--------------v 
  enum ee2 { a2 = 0, b2 = sizeof(int), c2 = sizeof(enum ee2) }; // Bad
  printf("%zu\n", sizeof(enum ee2)); // OK
}

Further

All declarations of structure, union, or enumerated types that have the same scope and use the same tag declare the same type. Irrespective of whether there is a tag or what other declarations of the type are in the same translation unit, the type is incomplete until immediately after the closing brace of the list defining the content, and complete thereafter. §6.7.2.3 4

Upvotes: 3

Sourav Ghosh
Sourav Ghosh

Reputation: 134406

Yes, void is also an incomplete type, which can never be complete.

Also, as mentioned in other answers, a declaration of a structure or union of unknown content (i.e., a forward declaration) is also an incomplete type. Remember, this turns to a complete type once it is declared with it's members (defining content).

To add, quoting C11, chapter §6.2.5/P1

[...] 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) or complete (having sufficient information).

P19,

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

and, P22,

An array type of unknown size is an incomplete type. It is completed, for an identifier of that type, by specifying the size in a later declaration (with internal or external linkage). A structure or union type of unknown content (as described in (6.7.2.3) is an incomplete type. It is completed, for all declarations of that type, by declaring the same structure or union tag with its defining content later in the same scope.

Upvotes: 3

RazaUsman_k
RazaUsman_k

Reputation: 704

An incomplete type is a type that describes an identifier but lacks information needed to determine the size of the identifier. An "incomplete type" can be

  • A structure type whose members you have not yet specified.

  • A union type whose members you have not yet specified.

  • An array type whose dimension you have not yet specified.

The void type is an incomplete type that cannot be completed

Upvotes: 6

dbush
dbush

Reputation: 225817

A forward declaration of a struct or union without a latter definition of its contents is also an incomplete type.

From section 6.2.5 of the C standard:

22 An array type of unknown size is an incomplete type. It is completed, for an identifier of that type, by specifying the size in a later declaration (with internal or external linkage). A structure or union type of unknown content (as described in 6.7.2.3) is an incomplete type. It is completed, for all declarations of that type, by declaring the same structure or union tag with its defining content later in the same scope.

Upvotes: 3

Related Questions