C Program to Demonstrate Double Pointers

C Program to Demonstrate Double Pointers

Double pointers are pointers that store the address of another pointer. They are used in C for dynamic memory allocation, passing pointers to functions, and handling multidimensional arrays. Understanding double pointers helps programmers manage complex data structures efficiently.

In this tutorial, we will explore how double pointers work, how to declare and use them, and practical examples. Each example includes a detailed explanation to clarify how memory addresses are manipulated.

Understanding the Problem

The goal is to demonstrate how double pointers allow access to the value of a variable indirectly through multiple levels of indirection. For example, a single pointer stores the address of a variable, while a double pointer stores the address of that pointer.

Double pointers are widely used when you need a function to modify the original pointer passed to it or when working with dynamically allocated multidimensional arrays.

Program 1: Basic Double Pointer Usage

This example shows how a double pointer can access and modify the value of an integer variable indirectly.

#include <stdio.h>

int main() {

    int num = 10;
    int *ptr = #   // Pointer to integer
    int **dptr = &ptr; // Double pointer

    printf("Value of num: %d\n", num);
    printf("Value via ptr: %d\n", *ptr);
    printf("Value via double pointer: %d\n", **dptr);

    **dptr = 20; // Modify num through double pointer
    printf("Modified value of num: %d\n", num);

    return 0;

}

In this code, ptr points to num, and dptr points to ptr. Accessing **dptr gives the value of num. By assigning a new value to **dptr, we modify num indirectly.

Program 2: Double Pointers with Functions

Double pointers are useful when a function needs to modify the pointer itself.

#include <stdio.h>

void changePointer(int **p) {

    static int newVal = 50;
    *p = &newVal; // Modify the original pointer

}

int main() {

    int num = 10;
    int *ptr = #

    printf("Before function call: %d\n", *ptr);

    changePointer(&ptr);

    printf("After function call: %d\n", *ptr);

    return 0;

}

Here, the function changePointer() accepts a double pointer int **p. By modifying *p, the function changes the original pointer ptr in main(), pointing it to a new value.

Program 3: Double Pointers with Dynamic Memory

Double pointers are also essential for dynamically allocating multidimensional arrays.

#include <stdio.h>
#include <stdlib.h>

int main() {

    int rows = 2, cols = 3;
    int **matrix;

    // Allocate memory for row pointers
    matrix = (int **) malloc(rows * sizeof(int *));

    // Allocate memory for each row
    for (int i = 0; i < rows; i++) {
        matrix[i] = (int *) malloc(cols * sizeof(int));
    }

    // Assign values
    int val = 1;

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

        for (int j = 0; j < cols; j++) {
            matrix[i][j] = val++;
        }

    }

    // Print matrix
    printf("Matrix elements:\n");

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

        for (int j = 0; j < cols; j++) {
            printf("%d ", matrix[i][j]);
        }

        printf("\n");

    }

    // Free memory
    for (int i = 0; i < rows; i++) {
        free(matrix[i]);
    }

    free(matrix);

    return 0;

}

This program demonstrates using a double pointer int **matrix to allocate a 2×3 matrix dynamically. Double pointers allow us to manage the memory of multiple rows efficiently.

FAQs

1. What is a double pointer?
A double pointer is a pointer that stores the address of another pointer, allowing multiple levels of indirection.

2. When should I use double pointers?
Use double pointers when you need to modify a pointer inside a function, work with dynamic multidimensional arrays, or manage complex data structures.

3. Can I have triple pointers?
Yes, C allows pointers to pointers to pointers (***ptr), but they are used rarely and usually for complex dynamic memory management.

4. Are double pointers faster than arrays?
Double pointers provide flexibility but may not be faster than static arrays. They are primarily used for dynamic memory and indirection, not for speed.

Conclusion

Double pointers are a powerful feature in C that enables multiple levels of indirection, dynamic memory management, and flexible function design. By understanding how **dptr works, you can manipulate values, change pointers inside functions, and allocate multidimensional arrays dynamically.

We explored examples ranging from basic double pointer usage to functions and dynamic memory allocation. Mastering double pointers is essential for advanced C programming and handling complex data structures.

References & Additional Resources

A curated collection of textbooks, tutorials, and documentation for learning pointers, including double pointers, in C.

  1. Kernighan, B.W., & Ritchie, D.M. (1988). The C Programming Language (2nd Edition). Prentice Hall – The foundational text covering pointers, arrays, functions, and core C programming concepts.
  2. GeeksforGeeks: Double Pointers in C – Explains the concept of double pointers (pointer to pointer), with examples and practical applications.
  3. Tutorialspoint: C Pointers – Beginner-friendly guide to pointers, including declaration, usage, and pointer arithmetic.
  4. Cprogramming.com: Pointer Basics – Practical examples on using pointers for arrays, functions, and dynamic memory.
  5. cplusplus.com: Pointers – Comprehensive reference for pointer concepts, arithmetic, and memory addressing in C.
  6. Stack Overflow: Understanding Double Pointers – Community discussion with explanations and examples for double pointer usage in various scenarios.
Scroll to Top