in Technology by
What is the use of a double pointer (pointer to pointer) in C?

1 Answer

0 votes
by

There is a lot of application of double-pointer in C language but here I am describing one important application of double-pointer. If you want to create a function to allocate the memory and you want to get back the allocated memory from the function parameter, then you need to use the double-pointer in that scenario. See the below code,

#include<stdio.h>
#include<stdlib.h>
void AllocateMemory(int **pGetMemory,int n)
{
int *p = malloc(sizeof(int)*n);
if(p == NULL)
{
*pGetMemory = NULL;
}
else
{
*pGetMemory = p;
}
}
int main()
{
int *arr = NULL;
int len = 10;
int i =0;
//Allocate the memory
AllocateMemory(&arr,len);
if(arr == NULL)
{
printf("Failed to allocate the memory\n");
return -1;
}
//Store the value
for(i=0; i<len; i++)
{
arr[i] = i;
}
//print the value
for(i=0; i<len; i++)
{
printf("arr[%d] = %d\n",i, arr[i]);
}
//free the memory
free(arr);
return 0;
}

Output:

double pointer application

 

Related questions

0 votes
    To open the table, on the icon that has the required table name in Tables Pane of Database Window (A) Drag ... click (D) Triple Click Select the correct answer from above options...
asked Dec 24, 2021 in Education by JackTerrance
0 votes
    Which is a valid declaration within an interface? A)protected short stop = 23; b)final void madness(short stop); ... (double duty);? Select the correct answer from above options...
asked Dec 1, 2021 in Education by JackTerrance
0 votes
    What is Indirection and Increment/Decrement operators with a pointer in C?...
asked Jan 24, 2021 in Technology by JackTerrance
0 votes
    How can we do Pointer comparison in C?...
asked Jan 24, 2021 in Technology by JackTerrance
0 votes
    How is a Program increment a pointer in C?...
asked Jan 24, 2021 in Technology by JackTerrance
0 votes
    Which type of pointer is the most convenient way of storing the raw address in C programming?...
asked Jan 23, 2021 in Technology by JackTerrance
0 votes
    How to declare a pointer to a function in C?...
asked Jan 22, 2021 in Technology by JackTerrance
0 votes
    What is the advantage of a void pointer in C?...
asked Jan 22, 2021 in Technology by JackTerrance
0 votes
    What is the usage of the NULL pointer in C?...
asked Jan 21, 2021 in Technology by JackTerrance
0 votes
    What is the size of a void pointer in C?...
asked Jan 21, 2021 in Technology by JackTerrance
0 votes
    What is a near pointer in C?...
asked Jan 21, 2021 in Technology by JackTerrance
0 votes
    What is a far pointer in C?...
asked Jan 21, 2021 in Technology by JackTerrance
0 votes
    What is the usage of the pointer in C?...
asked Jan 21, 2021 in Technology by JackTerrance
0 votes
    What is dangling pointer in C?...
asked Jan 21, 2021 in Technology by JackTerrance
0 votes
    What is a constant pointer in C?...
asked Nov 9, 2020 in Technology by JackTerrance
...