Do you agree that:
1 2 3 4
|
int bar = 42; // bar is not a pointer
int* foo; // foo is a pointer
foo = &bar; // foo points to memory location of bar
std::cout << *foo; // access value at the location of bar, i.e. access value of bar
|
If we are good so far, then lets do
nothing:
1 2 3 4
|
*foo // access value == dereference pointer
*(foo) // the parentheses are just for clarity
*(foo+0) // address + 0 == address
foo[0] // the very same value access
|
All four lines do exactly same. All four lines have an integer value.
The last just has a slightly different syntax.
int *gaz = new int[10];
The gaz does point to location of a int value, just like the foo before.
That integer value happens to be followed by other integer values in consecutive memory locations. If we add
sizeof(int)
to memory address of one of them, we do get address of the next integer value.
How much is
pointer+1
? It is the address stored in the pointer plus
sizeof(type in the pointers declaration).
You do have a pointer to type
example
object. When you do dereference that pointer, you get object of type
example
.
T * foo = new T[10];
foo+5 // address of the sixth T element in the array
*(foo+5) // the sixth T element in the array
foo[5] // the sixth T element in the array
1 2 3 4 5
|
// now the big reveal: T is example*
// an element in the array is a pointer
foo[5]->a // access member via pointer
*(foo[5]).a // dereference pointer and then access member of the object
foo[5][0].a // dereference pointer and then access member of the object
|