dereference a pointer and get the corresponding value.
example:
#include <stdio.h>
int main(int argc, char **argv) {
int *i_ptr;
int i, j;
i = 5;
i_ptr = &i;
j = *i_ptr;
printf("%d\n", j);
return 0;
}
prints 5
In C, you have memory locations (think kind of "boxes"), and values go inside them. Each memory location has an address, and you can store this address into a "pointer variable", which is declared with the type, followed by a *
. For example, i_ptr is pointer to integer (int *
), while i
and j
are integers. This means that in memory, three "boxes" have been allocated, two will be ready to contain actual numbers (i
and j
) and one (i_ptr
) will be ready to contain an address to another box, and you inform the compiler that the referred box will contain a number.
with &
you get the address of the box from the box name: &i
gives you the address of i
, and you can store it into i_ptr
.
When you have the address of the box and you want to to visit the box and get the value out you use *
, e.g. *i_ptr
gives you the value contained into the box at the address contained into i_ptr
.