首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >指针和数组:指向指针的指针

指针和数组:指向指针的指针
EN

Stack Overflow用户
提问于 2013-10-08 05:18:41
回答 3查看 215关注 0票数 0

我不明白下面的事情是怎么工作的?

代码语言:javascript
复制
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-p1不给sizeof(int)

EN

回答 3

Stack Overflow用户

回答已采纳

发布于 2013-10-08 10:00:06

为了解释输出,我注释了代码段的内容:

代码语言:javascript
复制
#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]

希望代码和评论能对你有所帮助。

如果代码是可编译的,并且我的屏幕上的输出是

代码语言:javascript
复制
ptr - p: 0x1
*ptr - a: 0x1
**ptr: 12
票数 3
EN

Stack Overflow用户

发布于 2013-10-08 05:22:51

在指针算法中,ptr - p将输出从pptr的元素的number,而不是从pptrsize。元素的大小与此无关。

顺便说一句,你的代码没有编译。下面是一个很小的例子来说明您的问题:

代码语言:javascript
复制
#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;
}

我的机器上的输出:

代码语言:javascript
复制
0x600b44 0x600b40 1
票数 2
EN

Stack Overflow用户

发布于 2013-10-08 05:22:27

指针算法是使用指向的元素的大小来完成的。由于您在++上使用了ptr,所以不管ptr是什么类型,区别都将是1

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/19239627

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档