Here, 0x7fffa8b8fc5b is the memory address where variable x is kept at and similar for variables y and z.
We can also define a pointer variable to store this address of x:
1 2 3
void* ptr; ptr = &x; PRINT(ptr);
Here, pointer variable ptr is defined as void* ptr.
But what does void* term actually mean? Well, the following section explain about this is detail.
Type of Pointer
Can a pointer have type?
In above example pointers to bool x, int y and double z all looks similar.
Indeed, that are same. Value of each of them is 8 byte integer (for 64 bit system).
No matter which variable is it pointing, its value is always 8 byte integer.
One difference we can notice is, the increment in memory address from x to z is not same.
There is increment of 1 between x and y. And increment of 4 between y and z.
This is because x is a bool variable which takes 1 bytes of space and y is int variable which takes 4 bytes of space in memory.
Basically, &x is the memory address of first byte where x is stored. And, &y is the memory address of first byte where y is stored. Similar for &z.
Note: Variables may not be stored consequiteley in the memory
So, the pointer variable which is pointing to the first byte of integer can be referred to as integer pointer. Similar for other variables.
void* used in above example is pointer without any specific type.
Lets clear this once again. From the perspective of a CPU, all pointers are treated the same. We just assign the different types to pointers such as bool*, int*, float* etc for the ease of use. One advantage is to know which datatype is the given pointer variable pointing to.
Another advantage is in Pointer Arthematic. Which is explained in following sections.
Type of pointer also comes in handy when we assign specific value to the variable pointed by that pointer.
1 2 3 4 5
int x = 7; int* ptr = &x; PRINT(*ptr); *ptr = 10; PRINT(*ptr);
Here, we were able to assign value 10 to int* ptr because we told compiler that ptr holds the variable of type int.:w
We have int* ptr. What might happen if we try to increment it (i.e. ptr++)?
1 2 3 4 5 6 7
int x = 1; int* ptr = &x; PRINT(ptr); ptr++; PRINT(ptr); ptr++; PRINT(ptr);