Search code examples
c++cpointersdouble-pointer

Double pointer in C


Anyone please elaborate what is happining here?

int main()
{
    int **p = 0;
//p=?  and why| *p=?  and why|**p=? and why

    ++p;
//p=?  and why| *p=?  and why|**p=? and why

    printf("%d\n", p);
return 1;
}

output:-

  • 4 (why?)

Solution

  • First of all, p is a pointer to a pointer-to-integer.

    int **p = 0;

    p = 0, *p = nothing, **p = less than nothing.

    ++p;

    Same as p = p + 1. Means the size of one pointer to a pointer-to-int further. A pointer is basically, at least on your OS, 32 bits length (4 bytes). p now points 4 bytes after 0. The value of p is 4.