Creating Pointers
You learned from the previous chapter, that we can get the memory address
of a variable with the reference operator &:
#include <stdio.h>
Int main() {
Int myAge = 43;
Printf(“%d\n”, myAge);
Printf(“%p\n”, &myAge);
Return 0;
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 are working with is assigned to the pointer:
#include <stdio.h>
Int main() {
Int myAge = 43; // An int variable
Int* ptr = &myAge; // A pointer variable, with the name ptr, that stores the
address of myAge
// Output the value of myAge (43)
Printf(“%d\n”, myAge);
// Output the memory address of myAge (0x7ffe5367e044)
Printf(“%p\n”, &myAge);
// Output the memory address of myAge with the pointer (0x7ffe5367e044)
Printf(“%p\n”, ptr);
Return 0;
Example explained
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 working with (int in our example).
Use the & operator to store the memory address of the myAge variable, and
assign it to the pointer.
Now, ptr holds the value of myAge’s memory address.
Dereference
In the example above, we used the pointer variable to get the memory
address of a variable (used together with the & reference operator).
You can also get the value of the variable the pointer points to, by using the *
operator (the dereference operator):
#include <stdio.h>
Int main() {
Int myAge = 43; // Variable declaration
Int* ptr = &myAge; // Pointer declaration
// Reference: Output the memory address of myAge with the pointer
(0x7ffe5367e044)
Printf(“%p\n”, ptr);
// Dereference: Output the value of myAge with the pointer (43)
Printf(“%d\n”, *ptr);
Return 0;
Pointers & Arrays
You can also use pointers to access arrays.
Consider the following array of integers:
Example
Int myNumbers[4] = {25, 50, 75, 100};
#include <stdio.h>
Int main() {
Int myNumbers[4] = {25, 50, 75, 100};
Int I;
For (I = 0; I < 4; i++) {
Printf(“%d\n”, myNumbers[i]);
}
Return 0;
Instead of printing the value of each array element, let’s print the memory
address of each array element:
Example
#include <stdio.h>
Int main() {
Int myNumbers[4] = {25, 50, 75, 100};
Int I;
For (I = 0; I < 4; i++) {
Printf(“%p\n”, &myNumbers[i]);
Return 0;
Note that the last number of each of the elements’ memory address is
different, with an addition of 4.
It is because the size of an int type is typically 4 bytes, remember:
#include <stdio.h>
Int main() {
Int myInt;
Printf(“%lu”, sizeof(myInt));
Return 0;
How Are Pointers Related to Arrays
Ok, so what’s the relationship between pointers and arrays? Well, in C, the
name of an array, is actually a pointer to the first element of the array.
Confused? Let’s try to understand this better, and use our “memory address
example” above again.
The memory address of the first element is the same as the name of the
array:
#include <stdio.h>
Int main() {
Int myNumbers[4] = {25, 50, 75, 100};
// Get the memory address of the myNumbers array
Printf(“%p\n”, myNumbers);
// Get the memory address of the first array element
Printf(“%p\n”, &myNumbers[0]);
Return 0;