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

2

5

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

Dnieper answered 12/10, 2010 at 18:15 Comment(3)
C99 uses reference type? Really? (If you have the C99 spec available, 6.2.5 discusses the C type system in depth; it begins with definitions of the three top-level type categories: object types, function types, and incomplete types)Dalmatic
My mistake, it doesn't. I'll remove that from the question. Thanks for the catch!Dnieper
That's exactly the section I was referring to, but didn't post in my question. ;) 6.2.5 mentions those top-level categories, but doesn't seem to ever say "what" goes inside of them. Function Types is easy to comprehend, but I'm still confused regarding the differences between Incomplete Type and Object Type. For Incomplete Types, "types that describe objects but lack information needed to determine their sizes" - what is an example of something that lacks info needed to determine its size?Dnieper
D
7

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;
};
Discommon answered 12/10, 2010 at 18:58 Comment(0)
S
0

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

Swaraj answered 12/10, 2010 at 18:29 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.