简体   繁体   English

指针和数组:指针的指针

[英]Pointers and Arrays: pointer to pointers

I am not getting how the following thing is working? 我不明白下面的事情是如何工作的?

void main()
{
    static int a[] = {10, 12, 23, 43, 43};
    int *p[] = {a, a + 1, a + 2, a + 3, a + 4};
    int **ptr = p;
    ptr++;
    printf("%d,%d,%d", ptr - p, *ptr - a, **ptr);
}

This is giving the output as 1 1 10 . 这给出的输出为1 1 10 I get that **ptr gives the value stored at ptr but why is ptr-p giving 1 should it not give sizeof(int) ? 我得到**ptr给出存储在ptr的值,但是为什么ptr-p给出1而不给出sizeof(int)呢?

To explain output I commented the of code snippet: 为了解释输出,我注释了代码片段的:

#include <stdio.h>                                                                                                 

int main()                                                                                                         
{                                                                                                                  
   // a is an array of integers                                                                                    
   static int a[]={10, 12, 23, 43, 43};                                                                            

   // p is an array of integer pointers. Each of the element, holds address of elements in array "a"               
   // p[0] = &a[0], p[1] = &a[1], p[2] = &a[2], p[3]=&a[3], p[4]=&a[4]                                             
   int *p[]={a, a + 1, a + 2, a + 3, a + 4};                                                                       

   // ptr is a pointer to an integer pointer. Ptr holds base address of array "p"                                  
   // ptr = &p[0]                                                                                                  
   // *ptr = *(&p[0]) = p[0]                                                                                       
   // **ptr = *(p[0]) = *(&a[0]) = a[0] = 10                                                                       
   int **ptr = p;                                                                                                  

   // ptr was pointing to zeroth element in the p array, which is p[0].                                            
   // Incrementing pointers, makes it to move by X bytes and hence point to the next element.                      
   // where X = sizeof(int *). int* is p's datatype.                                                               
   ptr++;                                                                                                          

   // ptr = &p[1]                                                                                                  
   // *ptr = *(&p[1]) = p[1]                                                                                       
   // **ptr = *(p[1]) = *(&a[1]) = a[1] = 12                                                                       

   // pointer difference is measured by the number of elements                                                     
   // since ptr points to p+1. difference is 1                                                                     
   printf("ptr - p: %p\n", (void*)(ptr - p) );                                                                     

   // ptr holds address of p+1. *ptr holds value of p[1], which as explained in the above holds address of a[1].   
   // Hence difference of (a+1) - a is 1                                                                           
   printf("*ptr - a: %p\n", (void* )(*ptr - a));                                                                   

   // ptr holds address of p+1. *ptr holds address of a[1]. **ptr holds value of a[1].                             
   printf("**ptr: %d\n", **ptr);                                                                                   
   return 0;                                                                                                       
}

Have printf statements and validate the comments I have provided in the program for better understanding. 拥有printf语句并验证程序中提供的注释以更好地理解。

For Example. 例如。 Compare p[0] and &a[0] . 比较p[0]&a[0] Compare *p[3] and a[3] . 比较*p[3]a[3]

Hope the code and the comments help you. 希望代码和注释对您有所帮助。

Provided code is compilable and output on my screen is 提供的代码是可编译的,并且在我的屏幕上的输出是

ptr - p: 0x1
*ptr - a: 0x1
**ptr: 12

In pointer arithmetic, ptr - p will output the number of elements from p to ptr , not the size from p to ptr . 在指针算法中, ptr - p将输出从pptr的元素 ,而不是从pptr大小 The size of elements is not relevant. 元素的大小无关紧要。

BTW, your code doesn't compile. 顺便说一句,您的代码无法编译。 A minimal example to illustrate your question looks like this: 一个最小的示例来说明您的问题,如下所示:

#include <stdio.h>

int main()
{
    static int a[] = {10,12,23,43,43};
    int *p = a;
    int *ptr = p;
    ptr++;
    printf("%p %p %d\n", (void*)ptr, (void *)p, ptr - p);
    return 0;
}

Output on my machine: 我的机器上的输出:

0x600b44 0x600b40 1

Pointer arithmetic is done using the size of the element pointed to. 指针算术是使用所指向元素的大小来完成的。 Since you used ++ on ptr , the difference is going to be 1 no matter what type ptr is. 由于您在ptr上使用了++ ,因此无论ptr是什么类型,其差异都将为1

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

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