简体   繁体   English

Array中的指针语法用法

[英]Pointer syntax usage in Array

I have some problem understanding the pointers syntax usage in context with two dimensional arrays, though I am comfortable with 1-D array notation and pointers, below is one of the syntax and I am not able to understand how the following expression is evaluated. 我对使用二维数组的上下文中的指针语法用法有一些问题,虽然我对1-D数组符号和指针感到满意,但下面是语法之一,我无法理解下面的表达式是如何计算的。

To access the element stored at third row second column of array a we will use the subscripted notation as a[2][1] other way to access the same element is 要访问存储在数组a的 第三行第二列的元素,我们将使用下标表示法作为[2] [1]其他方式来访问相同的元素是

*(a[2]+1)

and if we want to use it as pointers we will do like this 如果我们想用它作为指针,我们会这样做

*(*(a+2)+1)

Though I am able to understand the replacement of *(a[2]+1) as *(*(a+2)+1) but I don't know how this is getting evaluated. 虽然我能够理解*(a[2]+1)替换为*(*(a+2)+1)但我不知道如何评估它。 Please explain with example if possible. 如果可能,请举例说明。 Assume array is stored in row wise order and contain the following elements 假设数组按行顺序存储并包含以下元素

int a[5][2]={
               21,22,
               31,32
               41,42,
               51,52,
               61,62
             };

and base address of array is 100(just assume) so the address of a[2] is 108 (size of int =2(another assumption)) So the expression *(*(a+2)+1). How does it gets evaluated does it start from the inside bracket and if it does then after the first bracket we have the value to which 1 is being added rather than the address... :/ 数组的基址是100(只是假设)所以a [2]的地址是108(int的大小= 2(另一个假设))所以表达式*(*(a+2)+1). How does it gets evaluated does it start from the inside bracket and if it does then after the first bracket we have the value to which 1 is being added rather than the address... :/ *(*(a+2)+1). How does it gets evaluated does it start from the inside bracket and if it does then after the first bracket we have the value to which 1 is being added rather than the address... :/

To start of with 开始

a[i] = *(a+i);

So 所以

a[i][j] = *(a[i] +j)

and

a[i][j] = *(*(a+i) + j) 

How a[i] = *(a+i): a [i] = *(a + i)如何:

If a is a array then the starting address of the array is given by &a[0] or just a 如果a是一个数组,那么数组的起始地址由&a[0]或只是a

So when you specify 所以当你指定时

a[i] this will decay to a pointer operation *(a+i) which is, start at the location a and dereference the pointer to get the value stored in the location. a[i]这将衰减到指针操作*(a+i) ,即从位置a开始并取消引用指针以获得存储在该位置的值。

If the memory location is a then the value stored in it is given by *a . 如果存储器位置是a则存储在其中的值由*a给出。

Similarly the address of the next element in the array is given by 类似地,数组中下一个元素的地址由下式给出

&a[1] = (a+1); /* Array decays to a pointer */

Now the location where the element is stored in given by &a[1] or (a+1) so the value stored in that location is given by *(&a[1]) or *(a+1) 现在存储元素的位置由&a[1](a+1)因此存储在该位置的值由*(&a[1])*(a+1)

For Eg: 对于Eg:

int a[3];

They are stored in the memory as shown below: 它们存储在内存中,如下所示:

  a    a+1   a+2
------------------
| 100 | 102 | 104| 
------------------
 &a[0] &a[1] &a[2]

Now a is pointing to the first element of the array. 现在a指向数组的第一个元素。 If you know the pointer operations a+1 will give you the next location and so on. 如果您知道指针操作a+1将为您提供下一个位置,依此类推。

In 2D arrays the below is what the access is like : 在2D数组中,下面是访问的样子:

int arr[m][n];



  arr:
        will be pointer to first sub array, not the first element of first sub 
        array, according to relationship of array & pointer, it also represent 
        the array itself,

    arr+1:
        will be pointer to second sub array, not the second element of first sub 
        array,

    *(arr+1):
        will be pointer to first element of second sub array,
        according to relationship of array & pointer, it also represent second
        sub array, same as arr[1],

    *(arr+1)+2:
        will be pointer to third element of second sub array,

    *(*(arr+1)+2):
        will get value of third element of second sub array,
        same as arr[1][2],

A 2D array is actually a consecutive piece of memory. 2D数组实际上是一块连续的内存。 Let me take a example : int a[3][4] is representend in memory by a unique sequence of 12 integer : 让我举一个例子: int a[3][4]在内存中由12个整数的唯一序列表示:

a00 a01 a02 a03 a04 a10 a11 a12 a13 a14 a20 a21 a22 a23 a24
|                   |                   |
first row           second row          third row

(of course it can be extended to any muti-dimensional array) (当然它可以扩展到任何多维数组)

a is an array of int[4] : it decays to a pointer to int[4] (in fact, it decays to &(a[0]) ) aint[4]的数组:它衰减到指向int[4]的指针(事实上,它衰减为&(a[0])

a[1] is second row. a[1]是第二行。 It decays to a int * pointing to beginning of first row. 衰减到指向第一行开头的int *

Even if arrays are not pointer, the fact that they decay to pointers allows to use them in pointer arithmetic : a + 1 is a pointer to second element of array a. 即使数组不是指针,它们衰减到指针的事实允许在指针算术中使用它们: a + 1是指向数组a的第二个元素的指针。

All that explains why a[1] == *(a + 1) 所有这些都解释了为什么a[1] == *(a + 1)

The same reasoning can be applied to a[i] == *(a+i) , and from there to all the expressions in your question. 同样的推理可以应用于a[i] == *(a+i) ,并从那里应用到你问题中的所有表达式。

Let's look specifically to *(*(a+2)+1) . 让我们具体看看*(*(a+2)+1) As said above, a + 2 is a pointer to the third element of an array of int 4 . 如上所述, a + 2是指向int 4的数组的第三元素的指针。 So *(a + 2) is third row, is an array of int[4] and decays to a int * : &(a[2][0]) . 所以*(a + 2)是第三行,是int[4]的数组,并衰减为int *&(a[2][0])

As *(a + 2) decays to an int * , we can still use it as a base for pointer arithmetic and *(a + 2) + 1 is a pointer to the second element of third row : *(a + 2) + 1 == &(a[2][1]) . *(a + 2)衰减到int * ,我们仍然可以将它用作指针算术的基础,而*(a + 2) + 1是指向第三行的第二个元素的指针: *(a + 2) + 1 == &(a[2][1]) Just dereference all that and we get 只是取消引用所有这些,我们得到

*(*(a + 2) + 1) == a[2][1]

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

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