C Programming Tutorial

C Programming Tutorial

Pointer to Pointer in C (Double Pointer)

Hello Everyone, In this article, we will cover the concept of Pointer to Pointer in C Programming. A pointer to a pointer is a type of variable that stores the address of another pointer, which in turn points to the actual data. Whether you’re new to C or have some experience, this guide will help you to understand concet of Pointer to Pointer in C effectively. Let’s get started.

A pointer to pointer in C programming is a double pointer, which means it points to another pointer that in turn points to a data value. It is often used when working with multidimensional arrays or when manipulating data structures like trees and linked lists. In simple terms, a pointer to pointer is a variable that stores the address of another pointer variable.

Example to demonstrate concept of a pointer to pointer in c

#include <stdio.h>
int main() {
    int x = 10; // An integer variable
    int *ptr1; // A pointer to an integer
    int **ptr2; // A pointer to a pointer to an integer

    ptr1 = &x; // ptr1 points to the address of x
    ptr2 = &ptr1; // ptr2 points to the address of ptr1

    // Print the values and addresses of x, ptr1, and ptr2
    printf("Value of x: %d\\n", x);
    printf("Address of x: %p\\n", &x);
    printf("Value of ptr1: %p\\n", ptr1);
    printf("Address of ptr1: %p\\n", &ptr1);
    printf("Value of ptr2: %p\\n", ptr2);
    printf("Address of ptr2: %p\\n", &ptr2);

    // Accessing the value of x through ptr1 and ptr2
    printf("Value of x using ptr1: %d\\n", *ptr1);
    printf("Value of x using ptr2: %d\\n", **ptr2);

    return 0;
}

In this example, we have an integer variable x with the value 10. We also have a pointer to an integer, ptr1, and a pointer to a pointer to an integer, ptr2. We make ptr1 point to the address of x and ptr2 point to the address of ptr1. Then, we print the values and addresses of x, ptr1, and ptr2. Finally, we demonstrate how to access the value of x using both ptr1 (with a single dereference) and ptr2 (with a double dereference).

The output of the given example code will vary in terms of memory addresses because they depend on the system running the program. However, the general format of the output will be as follows:

Value of x: 10
Address of x: [address of x]
Value of ptr1: [address of x]
Address of ptr1: [address of ptr1]
Value of ptr2: [address of ptr1]
Address of ptr2: [address of ptr2]
Value of x using ptr1: 10
Value of x using ptr2: 10

In the output, [address of x], [address of ptr1], and [address of ptr2] will be replaced with the actual memory addresses of the variables in your system’s memory. The value of x accessed through ptr1 and ptr2 will remain the same as 10, as expected.

Here’s another example using pointer to pointer in C programming, this time with a function that modifies a value through a pointer to a pointer:

#include <stdio.h>
void increment(int **ptr_to_ptr) {
    // Increment the value pointed by *ptr_to_ptr
    (**ptr_to_ptr)++;
}

int main() {
    int x = 5;
    int *ptr1 = &x; // Pointer to an integer
    int **ptr2 = &ptr1; // Pointer to a pointer to an integer

    printf("Value of x before increment: %d\\n", x);

    // Call the increment function using ptr2
    increment(ptr2);

    printf("Value of x after increment: %d\\n", x);

    return 0;
}

Explanation:

  1. We define a function increment that takes a pointer to a pointer to an integer as its parameter (int **ptr_to_ptr).
  2. Inside the increment function, we dereference ptr_to_ptr twice with *ptr_to_ptr to access the value pointed to by the pointer to the integer. We then increment the value by 1.
  3. In the main function, we create an integer variable x with an initial value of 5.
  4. We create a pointer to an integer ptr1 and make it point to the address of x. Then, we create a pointer to a pointer to an integer ptr2 and make it point to the address of ptr1.
  5. We print the value of x before calling the increment function.
  6. We call the increment function, passing ptr2 as its argument. The function increments the value of x by 1 through the double pointer.
  7. We print the value of x after calling the increment function.

The output of this example will be:

Value of x before increment: 5
Value of x after increment: 6

In this example, we demonstrate how a pointer to a pointer can be used in a function to modify the value of a variable indirectly. This concept is often used when working with dynamic data structures, such as linked lists or trees, where a function may need to change the structure’s pointers to modify the data structure itself.

Here’s another example of using pointer to pointer in C programming, this time involving an array of strings:

#include <stdio.h>
int main() {
    // An array of strings (array of character pointers)
    char *names[] = {"Alice", "Bob", "Charlie", "David"};

    // A pointer to a pointer to a character
    char **ptr;

    // Initialize ptr to point to the first element of the array (address of "Alice")
    ptr = names;

    // Print the names using the ptr pointer
    for (int i = 0; i < 4; i++) {
        printf("Name %d: %s\\n", i + 1, *(ptr + i));
    }

    return 0;
}

In this example, we have an array of strings names, which is an array of character pointers. We also create a pointer to a pointer to a character, ptr. We initialize ptr to point to the first element of the array (the address of “Alice”).

We then use a for loop to iterate through the names and print them using the ptr pointer with an offset of i. The *(ptr + i) expression dereferences the pointer to access the string at the index i.

The output of this example will be:

Name 1: Alice
Name 2: Bob
Name 3: Charlie
Name 4: David

In this example, we have demonstrated how a pointer to a pointer can be used to access elements of an array of strings.

Share This Article