指针指向双精度的指针算术

时间:2018-05-10 10:34:06

标签: c pointers

我试图了解执行此操作时会发生什么

#include <stdio.h>

const int MAX = 3;

int main () {

   //double  var[] = {10.0, 100.1, 200.2};
   double  var[3];
   int  i;
   double *ptr;
   double **ptr2;


   for(i=0;i<MAX;i++)var[i] = 10.0*(double)(i+1);
   /* let us have array address in pointer */
   ptr = var;
   ptr2 = &ptr;

   for ( i = 0; i < MAX; i++) {

      printf("Address of var[%d] = %x\n", i, *ptr2 );
      printf("Value of var[%d] = %f\n", i, **ptr2 );

      /* move to the next location */
      ptr2++;
   }

   return 0;
}

此代码段在第二次迭代时给出了段错误

Address of var[0] = b0e04370                                                                                                                                                                 
Value of var[0] = 10.000000                                                                                                                                                                  
Address of var[1] = 0  

但是,如果我们调用ptr2

而不是致电ptr,那么这是有效的
for ( i = 0; i < MAX; i++) {

 printf("Address of var[%d] = %x\n", i, ptr );
 printf("Value of var[%d] = %f\n", i, *ptr );

  /* move to the next location */
  ptr++;
}

Address of var[0] = 3e9e89a0                                                                                                                                                                 
Value of var[0] = 10.000000                                                                                                                                                                  
Address of var[1] = 3e9e89a8                                                                                                                                                                 
Value of var[1] = 20.000000                                                                                                                                                                  
Address of var[2] = 3e9e89b0                                                                                                                                                                 
Value of var[2] = 30.000000 

1 个答案:

答案 0 :(得分:3)

它是段错误的,因为下一个地址实际上没有任何东西。

我将循环更改为此,现在它按照您期望的方式工作:

/* let us have array address in pointer */
ptr = var;
ptr2 = &ptr;

for (i = 0; i < MAX; i++) 
{
    printf("Address of var[%d] = 0x%x\n", i, (void*)*ptr2 );
    printf("Value of var[%d] = %f -- using ptr\n", i, *ptr);
    printf("Value of var[%d] = %f -- using ptr2\n", i, **ptr2);
    ptr++;
    // This updates ptr2, which was missed out
    ptr2 = &ptr;
}

用它打印以下内容:

Address of var[0] = a313c9b0
Value of var[0] = 10.000000 -- using ptr
Value of var[0] = 10.000000 -- using ptr2
Address of var[1] = a313c9b8
Value of var[1] = 20.000000 -- using ptr
Value of var[1] = 20.000000 -- using ptr2
Address of var[2] = a313c9c0
Value of var[2] = 30.000000 -- using ptr
Value of var[2] = 30.000000 -- using ptr2

您的代码在第二次迭代(而不是第一次)迭代的原因是

ptr = var;
ptr2 = &ptr;

有了这两行,ptr2ptr指向ptr

时不会保留var[0]的地址

让我们看看第二次迭代现在使用更新的代码:

ptr++;

使ptr指向var[1]

ptr2 = &ptr;

使ptr2保留ptr的地址,该地址现在指向var[1]
我猜这解释了。也许你可以自己尝试第三次迭代。