简体   繁体   English

C&C++中带有const限定符的指向数组的指针

[英]Pointer to array with const qualifier in C & C++

Consider following program:考虑以下程序:

int main()
{
    int array[9];
    const int (*p2)[9] = &array;
}

It compiles fine in C++ (See live demo here ) but fails in compilation in C. By default GCC gives following warnings.它在 C++ 中编译得很好(请参阅此处的现场演示)但在 C 中编译失败。默认情况下,GCC 给出以下警告。 (See live demo here ). (请参阅此处的现场演示)。

prog.c: In function 'main':
prog.c:4:26: warning: initialization from incompatible pointer type [enabled by default]
     const int (*p2)[9] = &array;

But If I use -pedantic-errors option:但是如果我使用-pedantic-errors选项:

gcc -Os -s -Wall -std=c11 -pedantic-errors -o constptr constptr.c

it gives me following compiler error它给了我以下编译器错误

constptr.c:4:26: error: pointers to arrays with different qualifiers are incompatible in ISO C [-Wpedantic]

Why it fails in compilation in C but not in C++?为什么它在 C 中编译失败但在 C++ 中没有? What C & C++ standard says about this? C & C++ 标准对此有何规定?

If I use const qualifier in array declaration statement it compiles fine in C also.如果我在数组声明语句中使用const限定符,它也可以在 C 中正常编译。 So, what is happening here in above program?那么,上面的程序中发生了什么?

GCC-gnu GCC-gnu

In GNU C, pointers to arrays with qualifiers work similar to pointers to other qualified types.在 GNU C 中,指向带有限定符的数组的指针的工作方式类似于指向其他限定类型的指针。 For example, a value of type int (*)[5] can be used to initialize a variable of type const int (*)[5] .例如, int (*)[5]类型的值可用于初始化const int (*)[5]类型的变量。 These types are incompatible in ISO C because the const qualifier is formally attached to the element type of the array and not the array itself .这些类型在 ISO C 中是不兼容的,因为const限定符正式附加到数组的元素类型而不是数组本身

C standard says that (section: §6.7.3/9): C 标准说(部分:§6.7.3/9):

If the specification of an array type includes any type qualifiers, the element type is so- qualified, not the array type .[...]如果数组类型的规范包含任何类型限定符,则元素类型是如此限定的,而不是数组类型。[...]

Now look at the C++ standard (section § 3.9.3/5):现在看看 C++ 标准(第 3.9.3/5 节):

[...] Cv-qualifiers applied to an array type attach to the underlying element type, so the notation “ cv T ,” where T is an array type, refers to an array whose elements are so-qualified. [...] 应用于数组类型的 Cv 限定符附加到底层元素类型,因此符号“ cv T ”(其中T是数组类型)是指元素被如此限定的数组。 An array type whose elements are cv-qualified is also considered to have the same cv-qualifications as its elements .元素为 cv 限定的数组类型也被认为具有与其元素相同的 cv 限定 [ Example : [示例

 typedef char CA[5]; typedef const char CC; CC arr1[5] = { 0 }; const CA arr2 = { 0 };

The type of both arr1 and arr2 is “array of 5 const char,” and the array type is considered to be const- qualified . arr1arr2的类型都是“5 个常量字符的数组并且数组类型被认为是常量限定的 —endexample] —结束示例]

Therefore, the initialization因此,初始化

const int (*p2)[9] = &array;  

is assignment of type pointer to array[9] of int to pointer to array[9] of const int .是将指向int array[9]的类型指针分配给指向 const int array[9] 的指针 This is not similar to assigning int * to a const int * where const is applied directly to the object type the pointer points to .这与将int *分配给const int * ,其中const直接应用于指针指向对象类型 This is not the case with const int(*)[9] where, in C, const is applied to the elements of the array object instead of the object the pointer points to.这不是const int(*)[9]情况,在 C 中, const应用于数组对象的元素而不是指针指向的对象。 This makes the above initialization incompatible.这使得上述初始化不兼容。

This rule is changed in C++.此规则在 C++ 中有所更改。 As const is applied to array object itself, the assignment is between same types pointer to const array[9] of int instead of type pointer to array[9] of int and pointer to array[9] of const int .作为const被施加到阵列对象本身,该分配是相同类型的指针指向的数组常量[9]之间int代替类型指针的阵列[9] int指针为const的阵列[9] int

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM