0

Consider this program:

#include <stdio.h>

int main()
{
    double *dp;
    double **dpp;

    dpp=&dp;

    dp++;
    dpp++;

   return 0;    
}

Suppose dp has address 100 and dpp has address 1000. What will the address of these pointers be after incrementing?

Maria Ines Parnisari
  • 16,584
  • 9
  • 85
  • 130
Sarao
  • 379
  • 1
  • 7
  • 18
  • 4
    Just run, print the values, and see for yourself. – ApproachingDarknessFish Feb 03 '14 at 05:18
  • @ ValekHalfHeart how can i assign initial address of 100 and 1000 to dp and dpp – Sarao Feb 03 '14 at 05:21
  • Whenever you're using a pointer, make sure it is initialized. You need `double d[2];` and `double *dp = d;` or thereabouts to make your code `dp++;` reliable and describable. In fact, you also need similar treatment for `dpp` too. At the moment, `dp++` could generate a fault (it does invoke undefined behaviour), and `dpp` doesn't point to anywhere determinate after the increment. – Jonathan Leffler Feb 03 '14 at 05:22
  • 1
    @Sarao You dont have to initialize the values as 100 and 1000 resp..Just print the values before and after incrementing the pointers..You will know the difference... – G one Feb 03 '14 at 05:24
  • Read [Pointer expressions: `**ptr++`, `*++*ptr` and `++**ptr` use](http://stackoverflow.com/questions/17752549/pointer-expressions-ptr-ptr-and-ptr-use) It uses pointer to pointer increments – Grijesh Chauhan Feb 03 '14 at 10:23

3 Answers3

3

The simplest thing is to make your program compilable and give it strictly defined behaviour, then run it.

#include <stdio.h>

int main(void)
{
    double   d[2]   = { 3.141593, 2.718128 };
    double  *dp     = &d[0];
    double  *dpa[2] = { &d[0], &d[1] };
    double **dpp    = dpa;

    printf("dp  = %p\n", (void *)dp);
    printf("dpp = %p\n", (void *)dpp);
    dp++;
    dpp++;
    printf("dp  = %p\n", (void *)dp);
    printf("dpp = %p\n", (void *)dpp);

    return 0;
}

Note how the code carefully ensures that the pointers always point to valid data. You could extend the printing to print *dpp (another pointer) and **dpp and *dp (both double values).

On my machine (Mac OS X 10.9.1, GCC 4.8.2, 64-bit compilation), the output is:

dp  = 0x7fff56945510
dpp = 0x7fff56945520
dp  = 0x7fff56945518
dpp = 0x7fff56945528

When compiled for 32-bit, the output is:

dp  = 0xbfffc600
dpp = 0xbfffc5f0
dp  = 0xbfffc608
dpp = 0xbfffc5f4

The jump of 8 in dp is a consequence of sizeof(double) == 8 (for both 32-bit and 64-bit compilations). The change in dpp of 4 for the 32-bit compilation and 8 for the 64-bit compilation is a consequence of sizeof(double *) == 4 for 32-bit and sizeof(double *) == 8 for 64-bit.

Jonathan Leffler
  • 730,956
  • 141
  • 904
  • 1,278
2

dp++ is undefined behavior, since dp was never initialized. There are no guarantees about what will happen.

What will probably happen, though relying on this would be a poor decision, is that the numerical value in memory of dp is incremented by sizeof(double), and the numerical value of dpp is incremented by sizeof(double *). sizeof(double) is probably 8, and sizeof(double *) is probably 4 or 8.

user2357112
  • 260,549
  • 28
  • 431
  • 505
0

double *dp => is pointer to variable of type double (size of dp is 4 bytes only)

double **dp1 => is a pointer to pointer which is pointing to double (size of dp1 is 4 bytes)

Pointer's size is always 4 bytes (assuming 32-bit machine; on a 64-bit machine, it's 8 bytes) i.e pointer size depends on how big the memory address is.

When you increment dp++, you increment pointer pointing to double whose size is 8 bytes so it increments by 8 bytes (100 => 108).

When you increment the pointer to pointer, its size is 4 bytes so it increments by 4 bytes only (1000 => 1004)

Pointer increments depend on the type of object it is pointing at.

In your code you haven't declared a variable of type double directly; you have initialized **dp1 which will will result in undefined behavior.

double a = 10.00;
dp = &a; 

add the above two line to understand your code better (just one of the way).

Jonathan Leffler
  • 730,956
  • 141
  • 904
  • 1,278
KARTHIK BHAT
  • 1,410
  • 13
  • 23