Pointers understanding

Pointers store address where a variable is allocated space in the memory.

int32 *p;
int32 q = 4;

Following statement stores the memory address for variable q in pointer p (a.k.a reference of q)

p = &q 

To print the value stored by p (i.e. q’s address)

std::cout << p;

To print the value stored at the memory address stored by p (output 4)

std::cout << *p;

It is also possible to store the address of pointer p in another pointer called a double pointer

int32 **r;
r = &p;

Following will output 4 as the value

std::cout << *p << std::endl;
std::cout << **r << std::endl;

Double * is used here to tell the C++ compiler that we want to print the value pointed by pointer p.

Privacy & Terms