Introduction
The indirection operator, also known as the dereference operator, is represented by the asterisk (*) in C. It is used to access the object stored at the memory address held by a pointer.
While the address-of operator (&) obtains the address of an object, the indirection operator performs the opposite operation—it accesses the object located at a given address.
The indirection operator is fundamental to pointer programming and is widely used in function calls, arrays, structures, dynamic memory allocation, and low-level programming.
What Is the Indirection Operator?
The indirection operator accesses the object pointed to by a pointer.
Syntax
*pointer
The operand must be a valid pointer. The result is the object stored at the memory location referenced by that pointer.
Relationship Between & and *
The address-of operator (&) and the indirection operator (*) are complementary.
| Operator | Purpose |
|---|---|
& |
Returns the address of an object. |
* |
Accesses the object stored at an address. |
In many situations:
Example
*&value
evaluates to the original object value.
Basic Example
The following program demonstrates how to access a variable through a pointer.
Example
#include <stdio.h>
int main(void)
{
int number = 100;
int *ptr = &number;
printf("%d\n", *ptr);
return 0;
}
Output
100
The expression *ptr retrieves the integer stored at the address contained in ptr.
Reading Through a Pointer
The indirection operator allows you to read the value of the object referenced by a pointer.
Example
int value = 50;
int *ptr = &value;
printf("%d\n", *ptr);
Although the program accesses the value indirectly through the pointer, the value printed is the same as value.
Modifying a Variable Through a Pointer
The object obtained through the indirection operator can be modified if it is not declared as const.
Example
#include <stdio.h>
int main(void)
{
int number = 10;
int *ptr = &number;
*ptr = 25;
printf("%d\n", number);
return 0;
}
Output
25
Changing *ptr changes the original variable because both refer to the same object.
Indirection with Different Data Types
The indirection operator works with pointers to any object type.
| Pointer Type | Dereferenced Type |
|---|---|
char * |
char |
int * |
int |
float * |
float |
double * |
double |
struct Student * |
struct Student |
Using the Indirection Operator with Function Parameters
Pointers allow functions to modify variables defined in the calling function.
Example
#include <stdio.h>
void increment(int *value)
{
(*value)++;
}
int main(void)
{
int number = 5;
increment(&number);
printf("%d\n", number);
return 0;
}
Output
6
The function modifies the original variable through the pointer.
Using the Indirection Operator with Arrays
Since an array name often decays to a pointer to its first element, the indirection operator can access array elements.
Example
int numbers[] = {10, 20, 30};
printf("%d\n", *numbers);
The expression *numbers is equivalent to numbers[0].
Multiple Levels of Indirection
Pointers can point to other pointers, creating multiple levels of indirection.
Example
int value = 100;
int *ptr = &value;
int **pptr = &ptr;
printf("%d\n", **pptr);
Here:
*ptraccessesvalue.*pptraccessesptr.**pptraccessesvalue.
Using the Indirection Operator with Structures
The indirection operator can access a structure object through a pointer.
Example
struct Student
{
int id;
};
struct Student student = {101};
struct Student *ptr = &student;
printf("%d\n", (*ptr).id);
The expression (*ptr).id accesses the structure member through the pointer.
ptr->id is a shorthand for (*ptr).id and is generally preferred because it is easier to read.
Dereferencing a NULL Pointer
A NULL pointer does not reference a valid object.
Example
int *ptr = NULL;
printf("%d\n", *ptr);
Dereferencing an Uninitialized Pointer
An uninitialized pointer contains an indeterminate value and must never be dereferenced.
Example
int *ptr;
printf("%d\n", *ptr);
Common Beginner Mistakes
- Confusing the indirection operator with the multiplication operator.
- Dereferencing NULL pointers.
- Dereferencing uninitialized pointers.
- Using the wrong pointer type for the referenced object.
- Forgetting parentheses in expressions such as
(*ptr).member.
Best Practices
- Always initialize pointers before dereferencing them.
- Check pointers against
NULLwhen appropriate. - Use descriptive pointer variable names.
- Understand the difference between reading and modifying through a pointer.
- Use the
->operator instead of(*ptr).memberwhen accessing structure members.
Summary
The indirection operator (*) accesses the object stored at the address contained in a pointer. It is the foundation of pointer programming and enables indirect access to memory.
| Expression | Description |
|---|---|
*ptr |
Access the object pointed to by ptr. |
*ptr = 10; |
Modify the referenced object. |
**pptr |
Access an object through a pointer to a pointer. |
(*ptr).member |
Access a structure member through a pointer. |
Mastering the indirection operator is essential for understanding pointers, arrays, structures, function parameters, dynamic memory allocation, and many advanced features of the C programming language.