如何理解复杂的数组的声明指针,&安培; [英] how to Understand complicated array declaration pointers, and &

查看:156
本文介绍了如何理解复杂的数组的声明指针,&安培;的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

int (*parry)[10] = &arr     // line 1
int *(&arrRef)[10] = ptrs   // line 2

关于1号线,招架是指向大小10的int数组那么是否意味着一个指针:

About line 1, parry is a pointer that points to an int array of size 10. So does it mean:


  • 粒子阵列[1] 点ARR的地址,

  • 粒子阵列[2] 点ARR的地址

  • ...

  • 粒子阵列[10] 点地址或常用3?

  • parray[1] points to the address of arr,
  • parray[2] points to address of arr
  • ...
  • parray[10] points to address or arr?

我什么时候会用1号线?
解决方案:

When would I use line 1? solution:

#include <iostream>
main(){
 int arr[10] = { 3, 54  };
 int (*parry)[10] = &arr;
 std::cout << (*parry)[0] << " " << (*parry)[1] << " "  << (*parry)[3] << "  " << parry[4] << std::endl;
 return 0;
 };

 output : 
 3, 54, 0, hex address of arr at index 4.

这似乎像什么里面招架[0]是指向ARR与索引相关的指针。所以招架[0] --->常用3 [0]

It seem like what inside parry[0] is a pointer that points to arr associated with the index. so parry[0] --- > arr[0].

关于2号线, arrRef 是十号的指针的int数组的引用。 arrRef由师生比参考。所以它意味着:

About line2, arrRef is a reference to an int array of size ten pointers. arrRef is refer to by ptrs. so does it mean:


  • ARRY [1]是一个int指针? ...

  • ARRY [10]是一个int指针?

可以在这个曾经用过的例子吗?

What example can this been used in?

推荐答案

现在我已经清理你的问题,我可以看到它是不是我原先以为。你说:

Now I've cleaned up your question, I can see it wasn't what I originally thought. You say:

粒子阵列是指向大小为10的int数组的指针

parray is a pointer that points to an int array of size 10

这么清楚你​​已经想通了顺时针/螺旋线/东西CDECL

so clearly you figured out the clockwise/spiral/cdecl stuff already.

所以,它的意思是:...粒子阵列[10]点ARR的地址

So does it mean: ... parray[10] points to address of arr

首先,阵列在C ++编制索引从零开始,所以你可以访问改编[0] ...改编[9] 如果有10个元素; 改编[10] 将是第十一届,所以是出界了。

Firstly, arrays in C++ are indexed starting from zero, so you can access arr[0] .. arr[9] if there are 10 elements; arr[10] would be the eleventh, so is out of bounds.

现在,让我们把你的句子分开:

Now, let's take your sentence apart:

粒子阵列是一个指针

parray is a pointer

正确的,这是不是一个数组,这是一个指针。现在,让我们考虑一下它是一个指针:

right, it isn't an array, it's a pointer. Now, let's consider what it is a pointer to:

尺寸为10的int数组

好吧,如果它的的这一点,那么 *粒子阵列必须是(参考)原始数组。

ok, if it points to that, then *parray must be (a reference to) the original array.

因此​​,(*粒子阵列)[0] 是数组等的第一个元素。

So, (*parray)[0] is the first element of the array, etc.

请注意,您可以轻松地在即将打印的事情了,看到你所得到的测试你的这一切直觉。你要么看到指针,能够比较地址,否则你会看到整数值,或者你会得到(希望信息)编译错误。试试吧!

Note that you can easily test your intuition about all this by just printing things out, and seeing what you get. You'll either see pointers, and be able to compare the addresses, or you'll see integer values, or you'll get (hopefully informative) compile errors. Try it out!

哦,

当我会用1号线?

只有当你需要重新就位,一般。例如,如果你想选择基于某种逻辑的两个不同的阵列之一,然后...进行进一步的逻辑上的任何选择。

Only if you need to re-seat it, in general. For example, if you want to choose one of two different arrays based on some logic, and then ... perform further logic on whichever was selected.

接下来,你说

arrRef是十号的指针的int数组的引用。

arrRef is a reference to an int array of size ten pointers.

正确!

arrRef由师生比是指

arrRef is refer to by ptrs

没有, arrRef 的数组,数组的大小为10,它的10个元素是指向到INT。 注意这是不是同一类型作为第一个阵!

No, arrRef refers to an array, the array has size 10, and its 10 elements are pointers-to-int. Note this is not the same type as the first array!

由于引用可以用相同的语法,它们指的是,我们可以使用 arrRef 作为数组的东西被使用。

Since references can be used with the same syntax as the thing they refer to, we can use arrRef as an array.

因此​​, arrRef [0] 是数组的第一个元素,它是一个指针到INT。

So, arrRef[0] is the first element of the array, and it is a pointer-to-int.

可以在这个曾经用过的例子吗?

What example can this been used in?

的唯一共同之所以使用参考到阵列是避免指针衰变,允许模板推断元件的数目

The only common reason for using reference-to-array is to avoid pointer decay, allowing templates to deduce the number of elements.

这篇关于如何理解复杂的数组的声明指针,&安培;的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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