Pointers in C

In C programming, a pointer is a variable that stores the memory address of another variable. Pointers are powerful tools that allow you to manipulate data directly in memory, making them a key part of C programming.

Definition

A pointer is a variable whose value is the memory address of another variable. Instead of holding data directly, a pointer holds the address where the data is located in memory.

Syntax:

                        
data_type *pointer_name;
                        
                    

How Pointers Work

To understand how pointers work, we need to know two main operations:

Example 1: Basic Pointer Usage

In this example, we demonstrate the basic use of pointers, where we assign the address of a variable to a pointer and access its value using the dereference operator.

Example:

                        
#include <stdio.h>
int main() 
{
    int num = 10;
    int *ptr = #  // Pointer ptr stores the address of num

    printf("Value of num: %d\n", num);
    printf("Address of num: %p\n", &num);
    printf("Value of ptr (Address of num): %p\n", ptr);
    printf("Value pointed to by ptr: %d\n", *ptr);

    return 0;               
}
                        
                    

Output

Value of num: 10
Address of num: 0x7ffee68d595c
Value of ptr (Address of num): 0x7ffee68d595c
Value pointed to by ptr: 10

Pointer Arithmetic

C allows pointer arithmetic, meaning you can increment, decrement, or add/subtract values to/from pointers. This is useful when working with arrays and dynamically allocated memory.

Pointer arithmetic is based on the size of the data type to which the pointer is pointing.

Example 2: Pointer Arithmetic

Example:

                        
#include <stdio.h>
int main()
{
    int arr[] = {10, 20, 30, 40};
    int *ptr = arr;  // Pointer pointing to the first element of the array
        
    printf("Value at arr[0]: %d\n", *ptr);
    ptr++;  // Move to the next element
    printf("Value at arr[1]: %d\n", *ptr);
    ptr++;  // Move to the next element
    printf("Value at arr[2]: %d\n", *ptr);
        
    return 0;
}
                        
                    

Output

Value at arr[0]: 10
Value at arr[1]: 20
Value at arr[2]: 30

Pointer to Pointer

In C, you can also have pointers that point to other pointers. These are known as "pointer to pointer" or "multi-level pointers." This is useful in handling multi-dimensional arrays or dynamic memory allocation.

Example 3: Pointer to Pointer

Example:

                        
#include <stdio.h>
int main() 
{
    int num = 100;
    int *ptr = #
    int **ptr2 = &ptr;  // Pointer to pointer

    printf("Value of num: %d\n", num);
    printf("Value pointed to by ptr: %d\n", *ptr);
    printf("Value pointed to by ptr2 (value of num): %d\n", **ptr2);

    return 0;               
}
                        
                    

Output

Value of num: 100
Value pointed to by ptr: 100
Value pointed to by ptr2 (value of num): 100

Null Pointer

A null pointer is a pointer that does not point to any valid memory location. It is often used to indicate that the pointer is not initialized or is invalid.

Example 4: Null Pointer

Example:

                        
#include <stdio.h>
int main() 
{
    int *ptr = NULL;  // Null pointer
        
    if (ptr == NULL) {
    printf("Pointer is null\n");
    } else {
    printf("Pointer is pointing to a valid memory location\n");
    }
        
    return 0;
}
                        
                    

Output

Pointer is null

Key Points