簡體   English   中英

指針和數組:指針的指針

[英]Pointers and Arrays: pointer to pointers

我不明白下面的事情是如何工作的?

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);
}

這給出的輸出為1 1 10 我得到**ptr給出存儲在ptr的值,但是為什么ptr-p給出1而不給出sizeof(int)呢?

為了解釋輸出,我注釋了代碼片段的:

#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;                                                                                                       
}

擁有printf語句並驗證程序中提供的注釋以更好地理解。

例如。 比較p[0]&a[0] 比較*p[3]a[3]

希望代碼和注釋對您有所幫助。

提供的代碼是可編譯的,並且在我的屏幕上的輸出是

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

在指針算法中, ptr - p將輸出從pptr的元素 ,而不是從pptr大小 元素的大小無關緊要。

順便說一句,您的代碼無法編譯。 一個最小的示例來說明您的問題,如下所示:

#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;
}

我的機器上的輸出:

0x600b44 0x600b40 1

指針算術是使用所指向元素的大小來完成的。 由於您在ptr上使用了++ ,因此無論ptr是什么類型,其差異都將為1

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM