- C Programming Tutorial
- C - Home
- Basics of C
- C - Introduction
- C - Features
- C - Basics
- C - History
- C - Structure of C Program
- C - Program Structure
- C - Hello World
- C - Compilation Process
- C - Comments
- C - Tokens
- C - Keywords
- C - Identifiers
- C - User Input
- C - Basic Syntax
- C - Data Types
- C - Variables
- C - Integer Promotions
- C - Type Conversion
- C - Type Casting
- C - Booleans
- Constants and Literals in C
- C - Constants
- C - Literals
- C - Escape sequences
- C - Format Specifiers
- Operators in C
- C - Operators
- C - Arithmetic Operators
- C - Relational Operators
- C - Logical Operators
- C - Bitwise Operators
- C - Assignment Operators
- C - Unary Operators
- C - Increment and Decrement Operators
- C - Ternary Operator
- C - sizeof Operator
- C - Operator Precedence
- C - Misc Operators
- Decision Making in C
- C - Decision Making
- C - if statement
- C - if...else statement
- C - nested if statements
- C - switch statement
- C - nested switch statements
- Loops in C
- C - Loops
- C - While loop
- C - For loop
- C - Do...while loop
- C - Nested loop
- C - Infinite loop
- C - Break Statement
- C - Continue Statement
- C - goto Statement
- Functions in C
- C - Functions
- C - Main Function
- C - Function call by Value
- C - Function call by reference
- C - Nested Functions
- C - Variadic Functions
- C - User-Defined Functions
- C - Callback Function
- C - Return Statement
- C - Recursion
- Scope Rules in C
- C - Scope Rules
- C - Static Variables
- C - Global Variables
- Arrays in C
- C - Arrays
- C - Properties of Array
- C - Multi-Dimensional Arrays
- C - Passing Arrays to Function
- C - Return Array from Function
- C - Variable Length Arrays
- Pointers in C
- C - Pointers
- C - Pointers and Arrays
- C - Applications of Pointers
- C - Pointer Arithmetics
- C - Array of Pointers
- C - Pointer to Pointer
- C - Passing Pointers to Functions
- C - Return Pointer from Functions
- C - Function Pointers
- C - Pointer to an Array
- C - Pointers to Structures
- C - Chain of Pointers
- C - Pointer vs Array
- C - Character Pointers and Functions
- C - NULL Pointer
- C - void Pointer
- C - Dangling Pointers
- C - Dereference Pointer
- C - Near, Far and Huge Pointers
- C - Initialization of Pointer Arrays
- C - Pointers vs. Multi-dimensional Arrays
- Strings in C
- C - Strings
- C - Array of Strings
- C - Special Characters
- C Structures and Unions
- C - Structures
- C - Structures and Functions
- C - Arrays of Structures
- C - Self-Referential Structures
- C - Lookup Tables
- C - Dot (.) Operator
- C - Enumeration (or enum)
- C - Structure Padding and Packing
- C - Nested Structures
- C - Anonymous Structure and Union
- C - Unions
- C - Bit Fields
- C - Typedef
- File Handling in C
- C - Input & Output
- C - File I/O (File Handling)
- C Preprocessors
- C - Preprocessors
- C - Pragmas
- C - Preprocessor Operators
- C - Macros
- C - Header Files
- Memory Management in C
- C - Memory Management
- C - Memory Address
- C - Storage Classes
- Miscellaneous Topics
- C - Error Handling
- C - Variable Arguments
- C - Command Execution
- C - Math Functions
- C - String Functions
- C - Static Keyword
- C - Random Number Generation
- C - Command Line Arguments
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):
- Pointer to Pointer with Simple Data Types:
C
#include <stdio.h>
int main() {
int num = 10; // A normal integer variable
int *ptr = # // 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.
- 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:
- Dynamic Memory Allocation:
- Pointer to pointer is used when you need to allocate memory dynamically for multi-dimensional arrays (e.g., 2D arrays, matrices).
- 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.
- 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.
