指针在C和const修饰词阵列; C ++ [英] Pointer to array with const qualifier in C & C++
问题描述
考虑下面的程序:
int main()
{
int array[9];
const int (*p2)[9] = &array;
}
它编译用C ++罚款(这里见现场演示),但未能在汇编C.默认情况下GCC给出以下警告。 (这里见现场演示)。
It compiles fine in C++ (See live demo here) but fails in compilation in C. By default GCC gives following warnings. (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;
不过,如果我使用 -pedantic-错误
选项:
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]
为什么在编译失败在C,但不是在C ++?什么C&放大器; C ++标准说这个呢?
Why it fails in compilation in C but not in C++? What C & C++ standard says about this?
如果我使用常量数组声明语句预选赛在C编译罚款也。那么,什么是发生在这里上面的程序?
If I use const qualifier in array declaration statement it compiles fine in C also. So, what is happening here in above program?
推荐答案
在GNU C,指针与数组工作预选赛类似于指向其他合格的类型。例如,类型的值 INT(*)[5]
可用于初始化类型 const int的(*)的变量[5]
。 这些类型在ISO C不兼容,因为常量
预选赛正式连接到阵列的元素类型,而不是该数组本身。
In GNU C, pointers to arrays with qualifiers work similar to pointers to other qualified types. For example, a value of type
int (*)[5]
can be used to initialize a variable of typeconst int (*)[5]
. These types are incompatible in ISO C because theconst
qualifier is formally attached to the element type of the array and not the array itself.
C标准说(部分:§6.7.3/ 9):
C standard says that (section: §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.[...]
现在看一下C ++标准(部分§3.9.3 / 5):
Now look at the C++ standard (section § 3.9.3/5):
[...]应用到阵列式CV-预选赛附加到底层的元素类型,所以符号 CV牛逼
,其中 T
是一个数组类型,指的是一个数组,其元素是如此的限定。 数组类型的元素是CV-资格也被认为具有相同的CV-资格的元素。 [示例的:
[...] Cv-qualifiers applied to an array type attach to the underlying element type, so the notation "
cv T
," whereT
is an array type, refers to an array whose elements are so-qualified. An array type whose elements are cv-qualified is also considered to have the same cv-qualifications as its elements. [ Example:
typedef char CA[5];
typedef const char CC;
CC arr1[5] = { 0 };
const CA arr2 = { 0 };
该型两种 ARR1
和 ARR2
是5为const char,数组和数组类型被认为是合格的const- 。 -endexample]
The type of both arr1
and arr2
is "array of 5 const char," and the array type is considered to be const- qualified. —endexample]
因此,初始化
const int (*p2)[9] = &array;
的类型是分配的指针数组[9]的 INT
的到的指针数组[9]为const INT
的。这不是类似于分配为int *
到 const int的*
,其中常量
直接施加到对象类型的指针指向。这是不是与[9] 其中,在C,常量
应用于 const int的(*)的情况下该阵列的元件对象,而不是该对象的指针指向。这使得上述的初始化不相容。
is assignment of type pointer to array[9] of int
to pointer to array[9] of const int
. This is not similar to assigning int *
to a const int *
where const
is applied directly to the object type the pointer points to. 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. This makes the above initialization incompatible.
这个规则在C ++中的变化。由于常量
被应用到数组对象本身,任务是常量数组同类型的指针之间[9]的 INT
的替代型的指针数组[9]的 INT
的和的指针数组[9]为const INT
的
This rule is changed in 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
.
这篇关于指针在C和const修饰词阵列; C ++的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!