指针的可用情况下在C未指定边界++数组(不是C) [英] Usable case of pointer to array with unspecified bounds in C++ (not in C)

查看:218
本文介绍了指针的可用情况下在C未指定边界++数组(不是C)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

考虑以下code:

int main() {
    int (*p)[]; // pointer to array with unspecified bounds

    int a[] = {1};
    int b[] = {1,2};

    p = &a; // works in C but not in C++
    p = &b; // works in C but not in C++

    return 0;
}

在纯C可以将指针分配给此类型的任何尺寸的数组的地址。但在C ++中,你不能。我发现一个情况下,当编译器允许将值分配给这样的指针:

In pure C you can assign a pointer to this type of address of an array of any dimension. But in C++ you can't. I found one case when compiler allows assign value to such pointer:

struct C
{
    static int v[];
};

int main() 
{
    int (*p)[] = &C::v; // works in C++ if 'v' isn't defined (only declared)
    return 0;
}

但找不到这个code的任何有用的情况。

But could not find any useful case of this code.

谁能给指针的一个有用的例子(在C ++)与未指定边界的数组?
抑或是只遗迹从C剩余?

Can anyone give an useful example (in C++) of pointer to array with unspecified bounds? Or is it only vestige remaining from C?

推荐答案

这样的指针不能参加指针运算,仍然可以做可能有用的东西都与 decltype reinter pret_cast 到另一个指针类型或使用intptr_t 。这是因为部分3.9p6说:

Such a pointer cannot participate in pointer arithmetic, potentially useful things that still can be done are to get its type with decltype or reinterpret_cast it to another pointer type or intptr_t. This is because section 3.9p6 says:

一个类类型(如类X )可能是在一个翻译单元一分不完整的,后来完成;类型为类X 是在两个点相同的类型。数组对象的声明类型可能不完整类的类型,因此不完整的数组;如果类类型在翻译单元完成以后,数组类型变得完整;在这两个点的数组类型是相同的类型。数组对象的声明类型可能是未知大小的数组,因此可以在一个翻译单元一分不完整的,后来完成;数组类型在这两个点(和约束的 T 未知的数组的 N个阵列 T )是不同的类型。 以大小未知,或由typedef声明中定义的类型的数组类型指针是未知大小的数组,就不能完成。

A class type (such as "class X") might be incomplete at one point in a translation unit and complete later on; the type "class X" is the same type at both points. The declared type of an array object might be an array of incomplete class type and therefore incomplete; if the class type is completed later on in the translation unit, the array type becomes complete; the array type at those two points is the same type. The declared type of an array object might be an array of unknown size and therefore be incomplete at one point in a translation unit and complete later on; the array types at those two points ("array of unknown bound of T" and "array of N T") are different types. 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.

5.3.1说:

请注意:间接通过一个指向一个不完整的类型(比的 CV 无效)是有效的。这样获得的左值可在有限的方式被使用(初始化的基准,例如);这个左值不能转换为prvalue,见4.1。

Note: indirection through a pointer to an incomplete type (other than cv void) is valid. The lvalue thus obtained can be used in limited ways (to initialize a reference, for example); this lvalue must not be converted to a prvalue, see 4.1.

由于数组到指针的衰减可在阵列左值,恕不另行转换为右值进行,留在注释中code DYP是正确的:

Since array-to-pointer decay can be performed on array lvalues without prior conversion to rvalue, the code dyp left in a comment is correct:

(*p)[i]

相关规则,从4.2:

Relevant rule, from 4.2:

左值或类型的 N T 或数组的右值< STRONG>约束 T 的未知的数组可以转换为指针类型一prvalue到 T ,其结果是一个指针数组的第一元素

An lvalue or rvalue of type 'array of N T" or "array of unknown bound of T" can be converted to a prvalue of type "pointer to T". The result is a pointer to the first element of the array.

这篇关于指针的可用情况下在C未指定边界++数组(不是C)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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