Pointers
A pointer is a variable that holds a memory location (an address)
Declaration
As any variable, pointers also can be of any data type. The *
operator is used before the name to indicate a pointer
int variable;
int *pointer_variable;
Also as any variable, it must be initialized before using it. This is done assigning the address of another variable
int variable = 5;
int *pointer_variable = &variable;
Dual Nature
A pointer can represent two things according to how it is used:
- If used without the
*
, it represents a memory location, an address - If used with the
*
, it represents the value at its location
int a = 4;
int *pointer;
pointer = &a;
printf("At address %p there is a value of %d", pointer, *pointer);
// Prints At address 000000000061FE14 there is a value of 4