指向不完整类型的指针可以不完整吗? [英] Can a pointer to an incomplete type be incomplete?

查看:137
本文介绍了指向不完整类型的指针可以不完整吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

int (*)[]可以是不完整的类型吗?

Can int (*)[] be an incomplete type?

C 2018 6.2.5 1说:

C 2018 6.2.5 1 says:

在翻译单元内的各个点上,对象类型可能是不完整(缺少足够的信息来确定该类型的对象的大小)或 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).

因此,如果知道一个类型的大小,则该类型似乎是完整的. 6.2.6.1 28指定某些类型的指针必须具有相同的大小(指向void和字符的指针,指向兼容类型的指针,指向结构的指针以及指向联合的指针),但是指向其他类型的指针可能会有所不同.

Thus it seems that if the size of a type is known, the type is complete. 6.2.6.1 28 specifies that certain types of pointers must have the same sizes (pointers to void and characters, pointers to compatible types, pointers to structures, and pointers to unions), but pointers to other types may vary.

在C的实现中,所有指针或指向int的数组的所有指针都具有相同的大小,那么int (*)[]的大小是已知的,因此将是完整的.例如,在对大型数组使用不同指针的实现中,大小是未知的,因此不完整.

In a C implementation where all pointers, or all pointers to arrays of int, have the same size, then the size of int (*)[] is known, so it would be complete. In an implementation that, say, uses different pointers for large arrays, the size would not be known, so it is incomplete.

作为 MM指出,根据6.7.2.1 3.中的约束,除最终的灵活数组成员外,结构不得包含类型不完整的成员,这表明指针大小为一种的实现必须接受struct { int (*p)[]; }而对于此类数组,具有不同大小的实现必须诊断出违反约束的情况. (这反过来意味着这样的声明不是严格符合C的一部分.)

As M.M points out, a structure must not contain a member with incomplete type, except a final flexible array member, per a constraint in 6.7.2.1 3. This suggests that an implementation with one size of pointers must accept struct { int (*p)[]; } while an implementation that has different sizes for such arrays must diagnose a constraint violation. (This in turn means such a declaration is not part of strictly conforming C.)

推荐答案

未知大小的数组不完整:

An array of unknown size is incomplete:

大小未知的数组类型是不完整的类型.对于该类型的标识符,可以通过在以后的声明(带有内部或外部链接)中指定大小来完成.

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

但是类型int (*)[]并非不完整:它是大小未知的int数组的指针.
指针的大小众所周知:

The type int (*)[] however is not incomplete: It's a pointer of an array of int of unknown size.
And a pointer has a well known size:

printf ("Size %d\n", sizeof(int (*)[]));

6.2.5/23:如果类型不是不完整的并且不是可变长度数组类型,则该类型具有已知的常量大小.

6.2.5/23: A type has known constant size if the type is not incomplete and is not a variable length array type.

此外,由于数组语义,您甚至可以取消引用它:

Furthermore you can even dereference it, thanks to the array semantics:

typedef int (*T)[];
...
int a[10];
for (int i=0; i<10; i++) a[i]=i;
T p=a;
for (int i=0; i<10; i++) printf ("%d ",(*p)[i]);
printf ("\n");

编辑

此外,指针始终是完整类型.它在6.2.5/20中写为黑底白字:

Edit

In addition, a pointer is always a complete type. It's written black on white in 6.2.5/20:

指针类型可以从函数类型或对象类型派生, 称为引用类型.指针类型描述了一个对象,该对象的 值提供对引用类型的实体的引用.一种 从引用类型T派生的指针类型有时被称为 指向T的指针".从引用中构造指针类型 类型称为指针类型派生". 指针类型是 完整的对象类型.

A pointer type may be derived from a function type or an object type, called the referenced type. A pointer type describes an object whose value provides a reference to an entity of the referenced type. A pointer type derived from the referenced type T is sometimes called ‘‘pointer to T’’. The construction of a pointer type from a referenced type is called ‘‘pointer type derivation’’. A pointer type is a complete object type.

这篇关于指向不完整类型的指针可以不完整吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆