Table of Contents
In this lesson, you will learn about pointers, how to use them, and the advantages and disadvantages of using pointers in a C program.
What is a pointer in programming?
A pointer is a variable that contains the memory address of another variable, that simple. For example, a char variable stores the value of a char. However, a char pointer stores the address of another char variable.
As you have already learned from previous lessons, a variable is a memory location that stores a value. This location can be accessed by using the (&) operator before the variable name.
#include <stdio.h> int main () { int myVar = 10; printf ("The value of the variable myVar is %d\n", myVar); printf ("The address of the variable myVar is %p\n", &myVar); return 0; }
Output
The value of the variable myVar is 10
The address of the variable myVar is 0x7ffe31324f6c
Declaring a pointer in C
Declaring a pointer in C is similar to declaring a variable. Besides, an asterisk must precede the variable name, so the compiler understands that it’s a pointer, not a regular variable.
int *intPointer; // pointer to an integer double *doublePointer; // pointer to a double float *floatPointer; // pointer to a float char *charPointer; // pointer to a character
Although the variables above are declared as an int, double, float, and char, the Data Type of these variables is a pointer Data Type.
Example of pointers in C
#include <stdio.h> int main () { int intVariable = 10; int *intPointer; intPointer = &intVariable; printf ("The address of intVariable is %x\n", &intVariable); printf ("The address stored in intPointer is %x\n", intPointer); printf ("Value of *intPointer is %d\n", *intPointer); return 0; }
Output
The address of intVariable is 11526b4The address stored in intPointer is 11526b4Value of *intPointer is 10
Pointer arithmetic operations in C
Since pointers are numeric values, you can perform arithmetic operations on them, just as we do on numeric values. The arithmetic operations that can be performed on a pointer are:
- Increment
- Decrement
- Addition
- Subtraction
Increment a pointer in C
Incrementing a pointer by a number, let’s say 1, will change the pointer location to the next memory location. However, the pointer will move by the size of its Data Type. For example, if we have a pointer of integer Data Type pointing to the address 100. After performing the increment operation, the new location for that pointer will be 104, not 101, as the Data Type int
is 4 bytes.
#include<stdio.h> int main () { int number = 10; int *intPointer; intPointer = &number; printf ("The address of intPointer is %u \n", intPointer); intPointer = intPointer + 1; // or intPointer+=1; printf ("After increment by 1, the new address of intPointer is %u \n", intPointer); return 0; }
Output
The address of intPointer is 2674317220
After increment by 1, the new address of intPointer is 2674317224
Notice the pointer value was incremented by 4.
Decrement a pointer in C
Just as incrementing a pointer, decrementing a pointer will move it to the previous memory location. Also, the pointer will be moved by the size of its Data Type. For example, an integer pointer pointing at location 100 will move to location 96 after decrementing by 1, as the Data time size of int
is 4 Bytes.
#include<stdio.h> int main () { int number = 10; int *intPointer; intPointer = &number; printf ("The address of intPointer is %u \n", intPointer); intPointer = intPointer - 1; // or intPointer-=1; printf ("After decrementing by 1, the new address of intPointer is %u \n", intPointer); return 0; }
The address of intPointer is 854384276After decrementing by 1, the new address of intPointer is 854384272
Pointer Addition in C
An addition operation to a pointer in C is performed based on the formula below:
newAddress = curentAddress + (number * seize_of(DataType))
For example, if you add 3 to a Data Type integer pointer, the addition will add the value of 3 X 4 ( int is 4 bytes) to the original address, which is 12.
#include<stdio.h> int main () { int number = 50; int *intPointer; intPointer = &number; printf ("The address of intPointer is %u \n", intPointer); intPointer = intPointer + 2; printf ("After Adding 2, the new address of intPointer is %u \n", intPointer); return 0; }
Output
The address of intPointer is 3819116836
After Adding 2, the new address of intPointer is 3819116844
Pointer subtraction in C
Like pointer Addition, substruction operation can be performed on a pointer in C. The formula is given below:
newAddress = curentAddress – (number * seize_of(DataType))
For example, if you subtract 3 from an integer Data Type pointer, the substruction will substruct the value of 3 X 4 ( int is 4 bytes) from the original address, which is 12.
#include<stdio.h> int main () { int number = 50; int *intPointer; intPointer = &number; printf ("The address of intPointer is %u \n", intPointer); intPointer = intPointer - 2; printf ("After Subtraction 2, the new address of intPointer is %u \n", intPointer); return 0; }
Output
The address of intPointer is 549661124
After Substructiong 2, the new address of intPointer is 549661116
Pointers comparison in C
In C language, pointers can be compared if the two pointers are pointing to the same array.
Below is a program to explain pointers comparison for the same type of pointer:
#include<stdio.h> int main () { int *pointerA, *pointerB; pointerA = (int *) 2; pointerB = (int *) 3; if (pointerB > pointerA) { printf ("pointerB is greater than pointerA"); } return (0); }
Advantage of pointers in C
- Pointer reduce the execution time of a program
- Pointers are the main elements of Data Structures, such as linked lists, queues, stacks, etc..
- Pointers can return more than one value to a function.
- Pointers help reduces the storage and complexity of a program.
- Pointers provide a way to extract the addresses of objects.
Disadvantage of Pointers
- Pointers bug can be hard to debug.
- Pointers might cause a segmentation fault if not initialized correctly.
- Pointers need to be freed explicitly to avoid a memory leak.
Summary
- A pointer is a variable that contains a memory location for another variable.
- Increment, decrement, Addition, and subtraction are the operations that can be performed on pointers.
- Pointers cannot be multiplied or divided.
- Two pointers can be compared to each other if both pointers are pointing at the same array.