C中不完整类型和对象类型的定义是什么?

发布于 2024-09-27 07:38:12 字数 144 浏览 6 评论 0原文

C中不完整类型对象类型的定义是什么?另外,您能否提供一些例子?

ANSI C99 在不同的地方提到了这两种类型类别,尽管我发现很难理解它们的确切含义(没有段落/句子明确定义它们是什么)。

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

为你鎻心 2024-10-04 07:38:12

让我们看看在线 C 标准(草案 n1256):

6.2.5 类型

1 存储在对象中或由函数返回的值的含义由
用于访问它的表达式的类型。 (声明为对象的标识符是最简单的此类表达式;类型在标识符的声明中指定。)类型分为对象类型(完全描述对象的类型),函数类型(描述函数的类型)和不完整类型(描述对象但缺乏确定其大小所需的信息的类型)。

不完整类型的示例:

struct f;    // introduces struct f tag, but no struct definition
int a[];     // introduces a as an array but with no defined size

您无法创建不完整类型的实例,但您可以从不完整类型创建指针和 typedef 名称:

struct f *foo;
typedef struct f Ftype;

要将不完整结构类型转换为对象类型,我们必须定义结构:

struct f
{
  int x;
  char *y;
};

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;
};
鹤仙姿 2024-10-04 07:38:12

我知道的类型有:

  • 不完整类型
  • 对象类型
  • 函数类型

这是一个示例(也在键盘上: 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;
}

struct incomplete 可以在另一个翻译单元中定义(具有确定的大小)。不过这个翻译单元只需要指针

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文