是一个指向未知大小的数组不完整? [英] Is a pointer to an array of unknown size incomplete?

查看:154
本文介绍了是一个指向未知大小的数组不完整?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

3.9 / 6 N3797:

3.9/6 N3797:

[...]

未知大小的数组的类型的指针,或者一种类型的定义
  通过typedef声明是未知大小的数组,不能
  完成。

The type of a pointer to array of unknown size, or of a type defined by a typedef declaration to be an array of unknown size, cannot be completed.

这听起来像一个指向未知大小的数组是一个不完整的类型。如果是这样,我们不能定义指针的一个目的是未知大小的阵列。但事实并非如此,因为我们可以定义未知边界的数组。

It sounds like a pointer to an array of unknown size is an incomplete type. If so we couldn't define an object of a pointer to array of unknown size. But it is not true, because we can define an array of unknown bound.

#include <iostream>

using std::cout;
using std::endl;

int (*a)[] = (int(*)[])0x4243afff;

int main()
{

}

它编译的罚款。

演示

DEMO

我们便无法做到这一点,如果它是不完整的类型。确实:
3.9 / 5:

We could't do it if it were incomplete type. Indeed: 3.9/5:

物体不应被定义为有一个不完整的类型

Objects shall not be defined to have an incomplete type

标准previously定义的一个不完整的类型如下3./5:

The Standard previously defined an incomplete types as follows 3./5:

这已宣告但尚未定义的类,在枚举类型
  某些情况下(7.2),或未知大小或不完整的数组
  元素类型,是一种不完全定义的对象类型。
  完全确定对象类型和无效类型是不完整
  类型(3.9.1)。

A class that has been declared but not defined, an enumeration type in certain contexts (7.2), or an array of unknown size or of incomplete element type, is an incompletely-defined object type. Incompletely defined object types and the void types are incomplete types (3.9.1).

这意味着指针不完全类型齐全。矛盾?

Which means the pointer to an incomplete type is complete. Contradiction?

那么,我错了,我的理由?

So where I'm wrong in my reasoning?

推荐答案

我觉得这个提法是有缺陷的。在您的code:

I think this wording is defective. In your code:

 int (*a)[];

的类型实际上是完成了。类型的* A 是不完整的。在我看来,(如DYP在评论中说的),该帖的目的是说,没有办法,后来在节目中, *一个将是一个前pression与完整的类型。

the type of a is actually complete. The type of *a is incomplete. It seems to me (as dyp says in comments) that the intent of the quote was to say that there is no way that later in the program, *a will be an expression with complete type.

背景:一些不完全类型以后可以完成例如由cdhowie和DYP建议:

Background: some incomplete types can be completed later e.g. as suggested by cdhowie and dyp:

extern int a[];
int b = sizeof a;  // error
int a[10];
int c = sizeof a;  // OK

INT(*一)[]; 以后将无法完成; 的sizeof * A 将永远是一个错误。

However int (*a)[]; cannot be completed later; sizeof *a will always be an error.

这篇关于是一个指向未知大小的数组不完整?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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