简体   繁体   English

指针到指针,指针值地址和2D数组访问之间的清晰度

[英]Clarity between Pointer-to-Pointer, Value-of-Pointed address and 2D-Array access

Can someone set the path straight on the disparities between these ubiquitous, as It can get a bit mind blowing. 有人可以为这些普遍存在的差异设置一条直路吗,因为它可能会让人有些震惊。

For example if I have a 2D array such as rec[3][2] , the following access means the same; 例如,如果我有一个2D数组,例如rec[3][2] ,则以下访问的含义相同;

rec[0][0] = **rec
rec[i][0] = **(rec + i)
*(*(rec + i) + j) = rec[i][j]

If this is the case then what are the meaning of these: 如果是这种情况,那么这些含义是什么:

#include <stdio.h>
double *recptr[3];
int i=1;

main()
{
 double n1=12.0;
 doublw n2=3.4;

 recptr[0]= &n1;
 recptr[1]= &n2;

 printf("Amt: %.2f\n", **(recptr + i));
}

What is **(recptr + i) , Is this access to 2D pointer or ponter-to-pointer reference? 什么是**(recptr + i) ,是否可以访问2D指针或点对点参考?

foo(ptr2ptr)double **ptr2ptr;
{
 int i=1, j=0;
 if(**(ptr2ptr +i) > **(ptr2ptr + j))
 {
  double *tmp= *(recptr +i);
 }
}

Again what is the difference between *(recptr +i) and **(ptr2ptr +i) ?! 同样, *(recptr +i)**(ptr2ptr +i)什么**(ptr2ptr +i) is the later also 2D access or access to pointer-2-ponter reference and the earlier the object pointed to? 后来还是2D访问或访问指针2 ponter参考并且对象指向的对象越早?

double *recptr[3];

This creates an array of 3 pointers to double (double*) . 这将创建一个包含3个指向double (double*)指针的数组。
Now, in the main() , we have double n1 = 12.0f and double n2 = 3.4f . 现在,在main() ,我们有double n1 = 12.0f和double n2 = 3.4f

Now we assign the address of n1 (&n1) to recptr[0] (First element of the array which just happens to point to doubles, so now it points to an address of a double (our n1) which is ok.) -> Please do note this is NOT a reference (in theory). 现在,我们将n1 (&n1)的地址分配给recptr[0] (该数组的第一个元素恰好指向double,因此现在它指向double的地址(我们的n1),可以)。->请注意,这不是参考(理论上)。 We are taking the address of n1. 我们正在使用n1的地址。

We do the same thing with n2, so now recptr[1] is a pointer that points to the address of n2. 我们对n2做同样的事情,所以现在recptr[1]是一个指向n2地址的指针。

Now we print **(recptr + i) -> This means the following: 现在我们打印**(recptr + i) ->这意味着:

We take the address of the first element of the array (recptr evaluates to the address of the aray), we offset this address by i (which just happens to be 1). 我们取数组第一个元素的地址(recptr求值为aray的地址),我们将该地址偏移i(恰好是1)。 Now please do note that we don't move 1 byte further, we move sizeof(double*) bytes further. 现在请注意,我们不会再移动1个字节,而是将sizeof(double*)个字节进一步移动。

Now, we dereference this position *(recptr + i) which is (address of first element + sizeof(double*) bytes) away and what do we see ? 现在,我们取消引用此位置*(recptr + i) ,即(第一个元素的地址+ sizeof(double*)个字节),然后看到什么? Well, it's a pointer to a double (Or in other words, it's just an address of n2). 好吧,它是一个指向double的指针(换句话说,它只是一个n2的地址)。 So we need to dereference yet again to actually see the value that this particular pointer points to, hence **(recptr + i) . 因此,我们需要再次解除引用才能实际看到此特定指针指向的值,因此是**(recptr + i)

So, the first dereference just gives us the correct "cell" of our recptr array but in that cell, there's a pointer to a double, so in order to get to it's value, we need to dereference yet again. 因此,第一次取消引用只是为我们的recptr数组提供了正确的“单元格”,但是在该单元格中,有一个指向double的指针,因此为了获得其值,我们需要再次取消引用。

(We then indeed print 3.40 since it's address happens to live in the second cell of recptr array.) (然后我们确实打印了3.40,因为它的地址恰好位于recptr数组的第二个单元格中。)

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

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