Pointer Basics
A pointer is simply a normal variable that holds the address of something else as its value. In other words, a pointer points to the address where something else can be found. Where you normally think of a variable holding an immediate values, such as int i = 40;
, a pointer (e.g. int *p = &i;
) would simply hold the address where 40
is stored in memory.
If you need the value stored at the memory address p
points to, you dereference p
using the unary '*'
operator, e.g. int j = *p;
will initialize j = 40
).
Since p
points to the address where 40
is stored, if you change that value at that address (e.g. *p = 41;
) 41
is now stored at the address where 40
was before. Since p
points to the address of i
and you have changed the value at that address, i
now equals 41
. However j
resides in another memory location and its value was set before you changed the value at the address for i
, the value for j
remains 40
.
If you want to create a second pointer (e.g. int *k;
) you are just creating another variable that holds an address as its value. If you want k
to reference the same address held by p
as its value, you simply initialize k
the same way you woul intialize any other varaible by assigning its value when it is declared, e.g. int *k = p;
(which is the same as assigning k = p;
at some point after initialization).
Pointer Arithmetic
Pointer arithmetic works the same way regardless of the type of object pointed to because the type
of the pointer controls the pointer arithmetic, e.g. with a char *
pointer, pointer+1
points to the next byte (next char
), for an int *
pointer (normal 4-byte integer), pointer+1
will point to the next int
at an offset 4-bytes after pointer
. (so a pointer, is just a pointer.... where arithmetic is automatically handled by the type
)
Chaining & and * Together
The operators available to take the address of an object and dereference pointers are the unary '&'
(address of) operator and the unary '*'
(dereference) operator. '&'
in taking the address of an object adds one level of indirection. '*'
in dereferening a pointer to get the value (or thing) pointed to by the pointer removes one level of indirection. So as @KamilCuk explained in example in his comment it does not matter how many times you apply one after the other, one simply adds and the other removes a level of indirection making all but the final operator superfluous.
(note: when dealing with an array-of-pointers, the postfix [..]
operator used to obtain the pointer at an index of the array also acts to derefernce the array of pointers removing one level of indirection)
Your Options
Given your declarations:
int i = 40;
int *p = &i;
int *k = ___;
and the pointer summary above, you have two options, both are equivalent. You can either initialize the pointer k
with the address of i
directly, e.g.
int *k = &i;
or you can initialize k
by assinging the address held by p
, e.g.
int *k = p;
Either way, k
now holds, as its value, the memory location for i
where 40
is currently stored.