磁盘阵列/指针/引用混乱 [英] Array/pointer/reference confusion
问题描述
可能重复:结果
<一href=\"http://stackoverflow.com/questions/3747938/confusion-over-c-pointer-and-reference-topic\">Confusion在C ++的指针和参考主题
假如我传递
int arr[10];
作为参数传递给函数。
as a parameter to a function.
是所有这些有效的函数原型?他们如何在参数方面,为什么不同?
Are all of these valid function prototypes? How do they differ in terms of arguments and why?
这是我所知道的,到目前为止(不知道正确与否)
This is what I know so far (not sure if correct or not)
1. void foo(int &arr); //a reference to an array, which preserve the size attribute?
2. void foo(int &arr[]); //the same (is it)?
3. void foo(int (&arr)[]); //I don't know
4. void foo(int &arr[10]); //is it the same as 2?
5. void foo(int (&arr)[10]);//no idea
6. void foo(int *arr); //a decayed pointer of the array, pointing to the first element of the array, losing the size attribute?
7. void foo(int *arr[]); //the same (is it?)
8. void foo(int (*arr)[]); //a pointer to an array, preserve the size
9. void foo(int *arr[10]); //the same as 7 (is it?)
10. void foo(int (*arr)[10]);//is the same as 8 (is it?)
11. void foo(int arr[]); //is the same as 6 (is it?)
12. void foo(int arr[10]); // is the same as 6 (is it?)
(我知道这需要一个漫长的解释,很抱歉,我完全糊涂了......)
(I know this will need a lengthy explanation, sorry, I'm totally confused...)
推荐答案
信息的第一个重要的一块是参数,其类型为 T的(有界或无界)阵列
转化为指针,以 T
。即无论 INT ARR []
和 INT改编[10]
转化为为int *改编
。注意,该变换仅在顶层阵列进行的,也就是说,它并不在 INT发生(*改编)[10]
,它是一个指向的数组INT。
The first important piece of information is that parameters whose type is a (bounded or unbounded) array of T
are transformed to pointers to T
. I.e. both int arr[]
and int arr[10]
are transformed to int * arr
. Note that the transformation is only performed on top-level arrays, i.e. it doesn't occur in int (*arr)[10]
, which is a pointer to an array of int.
此外,东西标识符绑定的不是事物的左侧,也就是更紧密地权为int *常用3 [10]
是一个数组,而 INT(* ARR)[10]
是一个指针。
Furthermore, things to the right of the identifier bind more closely than things to the left, i.e. int *arr[10]
is an array, whereas int (*arr)[10]
is a pointer.
最后的数组和指针引用是无效的,因为是指针和引用数组无限
Lastly, arrays of and pointers to references are invalid, as are pointers and references to unbounded arrays.
1. void foo(int &arr); // can't pass, reference to int
2. void foo(int &arr[]); // invalid, pointer to reference to int
3. void foo(int (&arr)[]); // invalid, reference to unbounded array of int
4. void foo(int &arr[10]); // invalid, pointer to reference to int
5. void foo(int (&arr)[10]); // can pass, reference to an array of int
6. void foo(int *arr); // can pass, pointer to int
7. void foo(int *arr[]); // can't pass, pointer to pointer to int
8. void foo(int (*arr)[]); // invalid, pointer to an unbounded array of int.
9. void foo(int *arr[10]); // can't pass, pointer to pointer to int
10. void foo(int (*arr)[10]); // can't pass, pointer to array of int
11. void foo(int arr[]); // can pass, pointer to int
12. void foo(int arr[10]); // can pass, same as above
使用改编
作为参数传递给富
将导致其衰减到一个指向它的第一个元素 - 传递给富
的值将是类型为int *
。请注意,您可以通过&放大器;改编
来的10号,在这种情况下类型的值 INT(*)[10]
将被通过,不会发生腐烂。
Using arr
as an argument to foo
will cause it to decay to a pointer to its first element -- the value passed to foo
will be of type int *
. Note that you can pass &arr
to number 10, in which case a value of type int (*)[10]
would be passed and no decay would occur.
这篇关于磁盘阵列/指针/引用混乱的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!