0

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

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呢?1sizeof(int)

4

3 回答 3

3

为了解释输出,我评论了代码片段:

#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
于 2013-10-08T10:00:06.270 回答
2

在指针算术中,ptr - p将输出to的元素数量,而不是to的大小。元素的大小无关紧要。pptrpptr

顺便说一句,您的代码无法编译。说明您的问题的最小示例如下所示:

#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
于 2013-10-08T05:22:51.483 回答
1

指针运算是使用指向的元素的大小来完成的。既然您使用++了 on ptr,那么1无论是什么类型,都会有所不同ptr

于 2013-10-08T05:22:27.473 回答