You learned from the previous chapter, that we can get the memory
address of a variable with the reference
printf("%d", myAge); // Outputs the value of myAge (43)
printf("%p", &myAge); // Outputs the memory address of myAge (0x7ffe5367e044)
In the example above,
&myAge is also known
as a pointer.
A pointer is a variable that stores the memory address of another variable as its value.
A pointer variable points to a data type (like
int) of the same
type, and is created with the
* operator. The address of the variable you're working with is assigned to the pointer:
int* ptr = &myAge; // A pointer variable, with the name ptr, that stores the address of myAge
// Output the value of myAge (43)
// Output the memory address of myAge (0x7ffe5367e044)
// Output the memory address of myAge with the pointer (0x7ffe5367e044)
Create a pointer variable with the name
ptr, that points to an
int variable (
myAge). Note that the type of the pointer has to match the type of the variable you're
& operator to store the memory address of the
myAge variable, and assign it to the pointer.
ptr holds the value of
myAge's memory address.
In the example above, we used the pointer variable to get the memory address of a variable (used together with the
& reference operator).
However, you can also get the value of the variable the pointer points to, by using the
* operator (the dereference operator):
int* ptr = &myAge; // Pointer declaration
// Reference: Output the memory address of myAge with the pointer (0x7ffe5367e044)
// Dereference: Output the value of myAge with the pointer (43)
Note that the
* sign can be confusing here, as it does two different things
in our code:
- When used in declaration (
int* ptr), it creates a pointer variable.
- When not used in declaration, it act as a dereference operator.
Why Should I Learn About Pointers? Pointers are important in C, because they give you the ability to manipulate the data in the computer's memory - this can reduce the code and improve the performance.
Note: Pointers must be handled with care, since it is possible to damage data stored in other memory addresses.
Good To Know: There are three ways to declare pointer variables, but the first way is mostly used:
int * myNum;