EDUCBA

EDUCBA

MENUMENU
  • Free Tutorials
  • Free Courses
  • Certification Courses
  • 600+ Courses All in One Bundle
  • Login

Double Pointer in C

Home » Software Development » Software Development Tutorials » C Programming Tutorial » Double Pointer in C

double pointer in c

Introduction to Double Pointer in C

In the C programming language, we have seen what pointers are and what are they used for. In C, a pointer means pointing directly to another variable. In general, Pointers are the variables that store the address of another variable. Whereas pointer to pointer which means a pointer stores the address of another pointer and this second pointer will be storing the address of the previous or first pointer which is also known as double-pointer in C. Therefore, double pointers are used when we want to store the address of the pointers. Usually, pointers are used to access the address of the variables that we want to get the value or access it.

How does Double Pointer work in C?

In this article, we will see how to declare double-pointer with syntax and example and also we will see how to use them in C programming language. So let us start from the syntax.

Start Your Free Software Development Course

Web development, programming languages, Software testing & others

Syntax:

int **pointer_var;

In the above syntax, we can see the variable pointer_var is prefixed with two stars (**) also known as indirection operator (*) for declaring the double-pointer.

So in general if the pointer is pointing to or referring to an object in memory then double-pointer is a pointer that would be pointing to or referring to another point where it is pointing to an object in memory. Let us see how this exactly works by below example and pictorial form:

object in memory

Examples to Implement Double Pointer in C

Below are the examples are mentioned:

Example #1

Code:

#include<stdio.h>
void main ()
{
int n = 20;
int *pr;
int **pr1;
printf("\nThe address of the variable n is: %x\n", &n);
pr = &n;
printf("\nThe address of variable n stored in single pointer is: %x\n",pr);
pr1 = &pr;
printf("\nThe address of pointer pr stored in double pointer is: %x\n",pr1);
printf("\nThe address of double pointer pr1 is: %x\n", &pr1);
printf("\nThe value stored at pointer pr: %d\n",*pr);
printf("\nThe value stored at another pointer pr1: %d\n",**pr1);
}

Output:

double pointer in c - 3

Explanation: In the above code, we have declared a variable “n” and initialized it to value “20” now we have declared a single pointer “*pr” and double pointer “**pr1” where the address of variable n will be stored in pointer”*pr” and the address of this single pointer “*pr” is stored in the pointer “**pr1” which is now a double-pointer. So when we print the value of a single pointer or double pointer the value will be 20 as double-pointer is indirectly pointing to the variable “n” and it will access its value. This can be shown as

Popular Course in this category
C Programming Training (3 Courses, 5 Project)3 Online Courses | 5 Hands-on Projects | 34+ Hours | Verifiable Certificate of Completion | Lifetime Access
4.5 (5,617 ratings)
Course Price

View Course

Related Courses
C++ Training (4 Courses, 5 Projects, 4 Quizzes)Java Training (40 Courses, 29 Projects, 4 Quizzes)

indirectly pointing

Example #2

In this article, let us see why and where double pointers can be used. There are several uses of a pointer to pointer where it is the address of a data. This can be explained by writing this code.

Code:

int n = 50;
int* p1 =&n;
int** p2= &p1;

If we see the above code if “n” is at the address 100 and pointer “p1” is pointing or assigned to the address of n (100) and p1 also has address 200 and pointer “p2” is now assigned to the address of p1 (200). Another use of a double pointer is when we want to allocate space in the matrix. This can be explained in the below code.

Code:

#include <stdlib.h>
int main(){
int **matrix;
int row=5,col=5;
int i;
matrix = (int**)malloc(row*sizeof(int*));
for (i=0;i<row;i++)
matrix[i]= (int*)malloc(col*sizeof(int));
}

Explanation: In the above code, as “matrix” is a double pointer it uses malloc function which dynamically allocates memory for the matrix of 5 rows and 5 columns. As we know that in the code “matrix” is integer data type so integer pointer can be used in the starting of the array as the address of the “matrix” pointer is an integer. Therefore, in the same way, a pointer to an integer pointer can have the starting address in the array of an integer as that is also an integer.

Example #3

Double pointers can also be used when we want to alter or change the value of the pointer. In general double pointers are used if we want to store or reserve the memory allocation or assignment even outside of a function call we can do it using double pointer by just passing these functions with ** arg. As we did it in the previous code. Let us consider an example where we want to change or update a character from a function.

Code:

void func(char ch)
{
ch = 'B';
}
int main()
{
char ptr;
ptr = 'A';
printf("%c", ptr);
func(ptr);
printf("%c\n", ptr);
}

The above code will not execute as we have passed the value to the function so this can be done by using pointers while passing by reference.

Code:

#include<stdio.h>
void func( char *p)
{
*p = 'Y';
}
int main()
{
char *p;
p = (char *)malloc(sizeof(char) * 1);
*p = 'X';
printf("%c\n", *p);
func(p);
printf("%c\n", *p);
}

Output:

double pointer in c - 4

Explanation: So in the above code, it will allow you to update the character at the pointer “p” with value “X” to the value “Y”.

Conclusion

In this article, we can conclude that pointers are also variables that can store values. But a pointer usually stores the value as the address of another variable. So commonly we can define double-pointer as pointer to pointer, which means a pointer stores the address of another pointer. In this way, double pointers are used in allocating the memory or assigning the value as the address of another pointer to access the value even outside the function call bypassing the reference to the argument using ** arg.

Recommended Article

This is a guide to Double Pointer in C. Here we discuss how Double Pointer works in C and examples for better understanding. You can also go through our other related articles to learn more –

  1. Pointers in C
  2. Nested if Statement in C
  3. C Literals
  4. Void Pointer in C

All in One Software Development Bundle (600+ Courses, 50+ projects)

600+ Online Courses

50+ projects

3000+ Hours

Verifiable Certificates

Lifetime Access

Learn More

0 Shares
Share
Tweet
Share
Primary Sidebar
C Programming Tutorial
  • Pointers
    • Pointers in C
    • Null pointer in C
    • Function Pointer in C
    • Double Pointer in C
    • Void Pointer in C
    • Const Pointer in C
    • Dangling Pointers in C
    • Pointer Arithmetic in C
  • Basic
    • Introduction to C
    • What is C
    • Career in C Programming
    • Advantages of C
    • How to Install C
    • Best C Compilers
    • Data Types in C
    • Variables in C
    • C Keywords
    • C Command
    • Command Line Arguments in C
    • C Literals
    • Constants in C
    • Unsigned Int in C
    • String in C
  • Operators
    • C Operators
    • Arithmetic Operators in C
    • Relational Operators in C
    • Assignment Operators in C
    • Logical Operators in C
    • Conditional Operator in C
    • Modulus Operator in C
    • Ternary Operator in C
    • Address Operator in C
    • Unary Operator in C
    • Operators Precedence in C
    • Left Shift Operator in C
  • Control Statement
    • Control Statements in C
    • If Statement in C
    • If-else Statement in C
    • Else if Statement in C
    • Nested if Statement in C
    • #else in C
    • Structure Padding in C
    • Nested Structure in C
    • Continue Statement in C
    • Break Statement in C
    • Switch Statement in C
    • Goto Statement in C
  • Loops
    • Loops in C
    • For Loop in C
    • While Loop in C
    • Do While Loop in C
    • Nested Loop in C
    • Infinite Loop in C 
  • Function
    • Math Functions in C
    • Hashing Function in C
    • Recursive Function in C
    • Power Function in C
    • fputs in C
    • C puts() Function
    • fprintf() in C
    • fseek() in C
    • Stderr in C
    • ASCII Value in C
    • strcat() in C
    • Inline Function in C
    • sizeof() in C
    • Function Prototype in C
    • C ftell()
  • Array
    • Arrays in C Programming
    • 2-D Arrays in C
    • 3D Arrays in C
    • Multidimensional Array in C
    • Array Functions in C
    • Strings Array in C
  • Sorting
    • Sorting in C
    • Heap Sort in C
  • Advanced
    • Constructor in C
    • Encapsulation in C
    • C Storage Classes
    • Static Keyword in C
    • File Handling in C
    • Queue in C
    • Hexadecimal in C 
    • typedef in C
    • Memory Allocation in C
    • Linked List in C
    • Volatile in C
    • Tokens in C
    • Expression in C
    • Regular Expression in C
    • Error Handling in C
    • Types of Errors in C
    • Preprocessor in C
    • Preprocessor Directives in C
    • fscanf() in C
    • #Pragma in C
    • #ifndef in C
    • #undef in C
    • Macros in C
  • C programs
    • Patterns in C Programming
    • Star Patterns in C
    • Number Patterns in C
    • Swapping in C
    • Reverse Number in C
    • Palindrome in C Program
    • Factorial in C
    • Fibonacci Series in C
    • Square Root in C
    • Random Number Generator in C
    • Prime Numbers in C
    • Escape Sequence in C
    • Reverse String in C
    • Leap Year Program in C
    • Anagram Program in C
    • Strong Number in C
    • String Concatenation in C
    • C Programming Matrix Multiplication
    • Decimal to Octal in C
    • Expression Evaluation in C
    • Decimal to Hexadecimal in C
  • Interview question
    • C Programming Interview Questions

Related Courses

C Programming Training Course

C++ Training Course

Java Training Course

Footer
About Us
  • Blog
  • Who is EDUCBA?
  • Sign Up
  • Corporate Training
  • Certificate from Top Institutions
  • Contact Us
  • Verifiable Certificate
  • Reviews
  • Terms and Conditions
  • Privacy Policy
  •  
Apps
  • iPhone & iPad
  • Android
Resources
  • Free Courses
  • Java Tutorials
  • Python Tutorials
  • All Tutorials
Certification Courses
  • All Courses
  • Software Development Course - All in One Bundle
  • Become a Python Developer
  • Java Course
  • Become a Selenium Automation Tester
  • Become an IoT Developer
  • ASP.NET Course
  • VB.NET Course
  • PHP Course

© 2020 - EDUCBA. ALL RIGHTS RESERVED. THE CERTIFICATION NAMES ARE THE TRADEMARKS OF THEIR RESPECTIVE OWNERS.

EDUCBA
Free Software Development Course

Web development, programming languages, Software testing & others

*Please provide your correct email id. Login details for this Free course will be emailed to you
Book Your One Instructor : One Learner Free Class

Let’s Get Started

This website or its third-party tools use cookies, which are necessary to its functioning and required to achieve the purposes illustrated in the cookie policy. By closing this banner, scrolling this page, clicking a link or continuing to browse otherwise, you agree to our Privacy Policy

EDUCBA

*Please provide your correct email id. Login details for this Free course will be emailed to you
EDUCBA Login

Forgot Password?

EDUCBA
Free Software Development Course

Web development, programming languages, Software testing & others

*Please provide your correct email id. Login details for this Free course will be emailed to you

Special Offer - C Programming Training Course Learn More