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

烈酒焚心 提交于 2019-12-10 17:28:49

问题


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:


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;
};



回答2:


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



来源:https://stackoverflow.com/questions/3917712/what-is-the-definition-of-incomplete-type-and-object-type-in-c

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!