C Programs Tutorials | IT Developer
IT Developer

C Programming - C Pointer to Pointer



Share with a Friend

C Programming - C Pointer to Pointer

C Pointer to Pointer

A pointer to pointer (also known as a double pointer) is a pointer that stores the address of another pointer. It allows you to work with indirect references to data, enabling more complex memory manipulations, such as dynamic memory allocation, handling multi-dimensional arrays, or manipulating arrays of pointers.

Concept of Pointer to Pointer:

  • A pointer stores the memory address of a variable.
  • A pointer to pointer stores the memory address of another pointer.

Syntax for Pointer to Pointer:

C

type **ptr;

  • type: The data type that the pointer is pointing to.
  • ptr: The name of the pointer to pointer.
  • **: The dereference operator applied twice.

Example of Pointer to Pointer (Double Pointer):

  1. Pointer to Pointer with Simple Data Types:

C

#include <stdio.h>

int main() {

    int num = 10;          // A normal integer variable

    int *ptr = &num;       // A pointer to an integer (stores address of num)

    int **pptr = &ptr;     // A pointer to a pointer to an integer (stores address of ptr)

    // Accessing the value of num using pointer to pointer

    printf("Value of num using pointer to pointer: %d\n", **pptr);  // 10

    return 0;

}

Output:

Value of num using pointer to pointer: 10

In this example:

  • num is an integer variable.
  • ptr is a pointer to num.
  • pptr is a pointer to ptr, i.e., a pointer to a pointer to num.
  1. Pointer to Pointer with Dynamic Memory Allocation:

One of the most common uses of a pointer to a pointer is in dynamic memory allocation, especially when you want to create a multi-dimensional array (like a 2D array) or work with pointers that point to dynamically allocated memory.

C

#include <stdio.h>

#include <stdlib.h>

int main() {

    int **arr;         // Declare pointer to pointer

    int rows = 3, cols = 3;

    // Allocate memory for 3 rows (array of pointers)

    arr = (int **)malloc(rows * sizeof(int *));

    // Allocate memory for each column in the rows

    for (int i = 0; i < rows; i++) {

        arr[i] = (int *)malloc(cols * sizeof(int));

    }

    // Assign values to the 2D array

    int value = 1;

    for (int i = 0; i < rows; i++) {

        for (int j = 0; j < cols; j++) {

            arr[i][j] = value++;

        }

    }

    // Print the 2D array using pointer to pointer

    printf("2D Array:\n");

    for (int i = 0; i < rows; i++) {

        for (int j = 0; j < cols; j++) {

            printf("%d ", arr[i][j]);

        }

        printf("\n");

    }

    // Free dynamically allocated memory

    for (int i = 0; i < rows; i++) {

        free(arr[i]);

    }

    free(arr);

    return 0;

}

Output:

2D Array:

1 2 3

4 5 6

7 8 9

In this example:

  • We dynamically allocate memory for a 2D array using pointers to pointers.
  • arr is a pointer to a pointer to an integer.
  • First, memory for rows (array of pointers) is allocated.
  • Then, memory for cols (each row) is allocated.
  • Finally, we assign values to the 2D array and print it.

Pointer to Pointer in Arrays:

A pointer to pointer can also be used to point to an array of pointers. This is particularly useful for working with arrays of strings (array of character pointers).

C

#include <stdio.h>

int main() {

    // Array of strings (array of character pointers)

    char *arr[] = {"Hello", "World", "Pointer", "To", "Pointer"};

    // Pointer to a pointer (array of pointers)

    char **ptr = arr;

    // Accessing strings using pointer to pointer

    for (int i = 0; i < 5; i++) {

        printf("%s\n", *(ptr + i));  // Dereferencing pointer to pointer

    }

    return 0;

}

Output:

Hello

World

Pointer

To

Pointer

In this example:

  • arr is an array of string literals (character pointers).
  • ptr is a pointer to the first element of arr, which is a pointer to the first string.
  • We can access each string in the array using pointer arithmetic and dereferencing ptr.

When to Use Pointer to Pointer:

  1. Dynamic Memory Allocation:
    • Pointer to pointer is used when you need to allocate memory dynamically for multi-dimensional arrays (e.g., 2D arrays, matrices).
  2. Handling Arrays of Strings:
    • Pointer to pointer is commonly used to handle an array of strings in C since each string is a character array, and an array of strings can be represented as an array of pointers to characters.
  3. Function Arguments:
    • Pointer to pointer can be used when you want to modify the value of a pointer (e.g., changing the address it points to) inside a function.

Key Points to Remember:

  • A pointer to pointer is a pointer that holds the address of another pointer.
  • It's often used when dealing with dynamic memory allocation or multi-dimensional arrays.
  • Dereferencing a pointer to pointer twice (**ptr) gives you the value stored at the location it ultimately points to.
  • Pointer arithmetic can be applied to pointers to pointers, though care should be taken to avoid errors such as accessing invalid memory locations.

Conclusion:

A pointer to pointer is a powerful concept in C that allows indirect referencing of memory locations. It provides flexibility for handling complex data structures, particularly when working with dynamically allocated memory, arrays of strings, or multi-dimensional arrays. Understanding how to use pointers to pointers can enhance your ability to manipulate memory and improve the efficiency of your programs.