hh4rsan
hh4rsan

Reputation: 13

pointer dereference: actual mechanism of compiler

I'm struggling a little bit with my understanding concerning the actual mechanism in dereferencing pointers (what the compiler actually does).

I red a lot through google and on here on stackoverflow, but I coulnd't quite get it yet :-(

I wrote a simple program with multiple pointers:

#include <iostream>

int main()
{
    int a = 5;
    int* ptr = &a;
    int** pptr = &ptr;
    int*** ppptr = &pptr;
    int**** p4tr = &ppptr;

    std::cout << "a = 5 \nint*ptr = &a \nint** pptr = *ptr\nint*** ppptr = &pptr\nint**** p4tr= &ppptr\n" << std::endl;
    std::cout << "a: " << a << std::endl;
    std::cout << "&a: " << &a << std::endl << std::endl;
    std::cout << "ptr: " << ptr << std::endl;
    std::cout << "*ptr: " << *ptr << std::endl;
    std::cout << "&ptr: " << &ptr << std::endl << std::endl;
    std::cout << "pptr: " << pptr << std::endl;
    std::cout << "*ptr: " << *pptr << std::endl;
    std::cout << "**pptr: "<< **pptr << std::endl;
    std::cout << "&pptr: " << &pptr << std::endl << std::endl;
    std::cout << "ppptr: " << ppptr << std::endl;
    std::cout << "*ppptr: " << *ppptr << std::endl;
    std::cout << "**pptr: " << **ppptr << std::endl;
    std::cout << "***pptr: " << ***ppptr << std::endl;
    std::cout<< "&pptr: " << &ppptr << std::endl << std::endl;
    std::cout << "p4tr: " << p4tr<< std::endl;
    std::cout << "*p4tr: " << *p4tr<< std::endl;
    std::cout << "**p4tr: " << **p4tr<< std::endl;
    std::cout << "***p4tr: " << ***p4tr<< std::endl;
    std::cout << "****p4tr: " << ****p4tr<< std::endl;
    std::cout << "&p4tr: " << &p4tr<< std::endl << std::endl;

    return 0;
}

Which gives me this on my machine:

a = 5 
int*ptr = &a 
int** pptr = *ptr
int*** ppptr = &pptr
int**** p4tr= &ppptr

a: 5
&a: 0x7fffe4db870c

ptr: 0x7fffe4db870c
*ptr: 5
&ptr: 0x7fffe4db8700

pptr: 0x7fffe4db8700
*ptr: 0x7fffe4db870c
**pptr: 5
&pptr: 0x7fffe4db86f8

ppptr: 0x7fffe4db86f8
*ppptr: 0x7fffe4db8700
**pptr: 0x7fffe4db870c
***pptr: 5
&pptr: 0x7fffe4db86f0

p4tr: 0x7fffe4db86f0
*p4tr: 0x7fffe4db86f8
**p4tr: 0x7fffe4db8700
***p4tr: 0x7fffe4db870c
****p4tr: 5
&p4tr: 0x7fffe4db86e8

What I figured out, how dereference works is: int* ptr = &a; tells the compiler that the "variable" ptr is of type "int*" (pointer to an interger; i.e. memory address) Hence, when I write *ptr, the compiler takes the value of ptr, takes it as an address and interpretes what is stored at the very address as type int.

So far, so good.

But what does int** pptr = &ptr actually mean to the compiler ? Does it mean pptr is of type " int** " ? or does it still mean pptr is of type " int* " (i mean, &ptr is as good a memory address as &a)

What does the second asterix actually mean to the compiler and why can't I write: "int* pptr = &ptr" (at least g++ won't let me do that)

thank you so much for your efforts, it hurts my brain, if things seem unlogical to me :-))

Upvotes: 1

Views: 284

Answers (1)

David Heffernan
David Heffernan

Reputation: 612814

But what does int** pptr = &ptr actually mean to the compiler? Does it mean pptr is of type int**?

Yes, pptr is of type int**. And int** is pointer to pointer to int. So *pptr has type int*, and **pptr has type int.

Why can't I write: int* pptr = &ptr?

Well, ptr has type int*. And so &ptr has type int** which is not assignment compatible with a variable of type int*. A pointer to int is a different type of thing to a pointer to pointer to int.

Upvotes: 2

Related Questions