Table of contents
Let's have a chat and dissect in depth about Pointers.
Now to understand pointers, Let's first refresh our knowledge of variables. If you've ever learned to program, you must have been taught/learned that a variable is a name given to a computer memory location to store data in a program, this data can be known or unknown based on the assignment of value to the variable.
To understand this, Let's declare a variable of type int for example;
int a; // a memory of 4bytes shall be located for variable a since it is an integer
a = 10; // value 10 will be stored in the memory address
Whenever you create a variable, memory has to be allocated to store that variable, the size of memory allocated depends on the type of variable. For example;
int = 4 bytes of memory
char = 1 byte of memory
float and double = 8 bytes of memory
What is a Pointer?
Let us use this code as our study case;
#include <stdio.h>
int main()
{
int num;
char letter[10];
printf("Address of num variable is: %x\n", &num);
printf("Address of letter variable is: %x\n", &letter);
return (0);
}
When this code is compiled and executed, the result shall be as follows;
Address of num variable is: 0x02bff5a400
Address of letter variable is: 0x02bff5a3f6
From the above code, We can draft out the definition of a pointer. We can therefore go ahead and define a pointer as follows;
A pointer can be defined as a variable that holds the address of another variable. A pointer can also be defined as a variable whose value is the memory address of another variable.
Declaration of Pointer
The basic syntax to declare a pointer is;
int *ptr;
This declares a pointer ptr
as an identifier of an object of type int
. This can also be succinctly stated as a ptr
to an int
A pointer variable points to a data type (like int
) of the same type, and is created with the asterisk (*)
operator.
Because the C language does not specify an implicit initialization for objects of automatic storage duration, care should often be taken to ensure that the address to which ptr
points to is valid; this is why it is sometimes suggested that a pointer be explicitly initialized to the null pointer value, which is traditionally specified in C with the standardized macro NULL
:
int *ptr = NULL;
Example:
int age = 20; //This is a variable of type int
int* ptr = &age // pointer variable of name ptr that stores of age
//Output value of age
printf("%d\n", age);
//Output memory address of age
printf("%p\n", ptr);
Here, we've created a pointer variable with the name ptr
that points to an int
variable (age)
. The type of pointer variable has to match the variable type, In this case, it is int
for the pointer and data type.
Note that we use the &
operator to store the memory address of age
and assign it to ptr
which will now hold the age's
memory address.
Dereferencing
Dereferencing is a term used when we try to access or manipulate data contained in a memory address pointed to by a pointer. Asterisk is still used in dereferencing.
The * operator does make pointers and dereferencing confusing as there are two entirely different uses for the operator.
The first time we use the operator is when we declare a specific variable as a pointer. Since we have to assign a data type to our pointers, we use the operator at that time.
Once a variable is declared as a pointer, we no longer need to use the * operator when referring to the pointer in our code. The program will remember that the variable is declared as such. For the sake of code readability, it’s highly advised to name a pointer something that leaves no doubt as to its being a pointer.
But when using dereferencing, we need to call upon the operator (*) differently. With dereferencing, we need to use the operator whenever we want to reference the value of the variable that the pointer is pointing to.
Example
int age = 20; //declaring variable of type int
int* ptr = &age; //declaring pointer
//Reference: Outputs memory address of age with pointer (0x7ffe5367e044)
printf("%p\n", ptr);
//Dereference: Output the value of age with pointer (20)
printf("%d\n", *ptr);
Conclusion
Pointers are a crucial element in programming especially in the languages of C and C++.
Pointers make programming simple, help in memory management (allocation and de-allocation of memory), enhance the execution speed of a program, and help traverse arrays and character strings among others.
Therefore, from this article, it is vital to conclude that pointers help us have the power to perform complex tasks in languages like C where everything has to be manually done.