• Status: Solved
• Priority: Medium
• Security: Public
• Views: 2006

# Use of double and triple pointer

Dear all,
I am confused with the double and triple pointer.Could anyone explain me when they are used .Please explain with some functions such as
Double_Pointer(int ** x){
}
Triple_Pointer(Struct ***tp;){
}
Sorry for the inconvenience.But like to get a clear idea...
TCSD
0
TCSD
1 Solution

Associate Director - Product EngineeringCommented:
You need a double-pointer when you want to pass a pointer to a function and get it modified. Meaning, not the value stored in the memory location that the pointer points to, but you want to modify the pointer to make it point to some other location.

e.g., you have a structure-pointer 'ptr' which is initially null. You want to allocate memory in a function and assign ptr to point to that location. As:

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

struct xyz
{
int a;
float b ;

} ;

void allocate ( struct xyz ** pointer_to_ptr )
{
* pointer_to_ptr = ( struct xyz * ) malloc ( sizeof ( struct xyz ) ) ;
* pointer_to_ptr -> a = 1 ;
* pointer_to_ptr -> b = 4.5 ;

} // end of allocate ()

void main ()
{
struct xyz * ptr = NULL ;
allocate ( &ptr ) ;
printf ( "%d %f", ptr -> a, ptr -> b ) ;

} // end of main ()

Similar concept holds for the triple-pointer.

One word of advice: stop calling it as double/ triple pointer. Its pointer-to-a-pointer and likewise.

Mayank.
0

Commented:

int i=10; /* a variale */

/* to point out a variable u ca use single pointer */

int *ptr;
ptr = &i;

/* to point ptr, u should define double pointer */
int **pptr;

pptr = &ptr;

/*same to point pptr, u should define tripple pointer */
int ***ppptr;

ppptr = &pptr;

0

Associate Director - Product EngineeringCommented:
Yup! Summed up well!
0

Commented:
Perhaps you know this already, but a common reason to use such constructs is to create arrays when you don't know the size at compile-time.

Consider the following:

struct xyz
{
int a;
float b ;
char string[23];
} ;

You could define an array as:
struct xyz array_of_struct[10];

You could also define an array of pointers to structures, as follows:

struct xyz **ptrs_to_struct;

and allocate the memory this way:

ptrs_to_struct = (struct xyz **)malloc(max_structures*sizeof(struct xyz *));

The statement above creates an array of pointers to struct xyz.  There are "max_structures" of pointers in the array - and max_structures could be set at run-time.  Now, all we have done is allocate space for the pointers, we still need space for the structs:

for (i=0;i<max_structures;++i)
{
ptrs_to_struct[i]=(struct xyz *)malloc(sizeof(struct xyz));
}

As you've probably guessed, this is also the way you can create multi-dimensional arrays.

For more discussion on this topic, see the C FAQ...

http://www.eskimo.com/~scs/C-faq/q6.16.html
}

0

Commented:
There are a few common uses of double pointers.

An array of strings...
char** argv

A 2-dimentional array is the same as a double ppinter.
int cipherBlock[ 4 ][ 4 ];

Reassigning a pointer within a function (arguably bad form):

void reassignPtr( char** ptr )
{
*ptr = newptr;
}
0

Commented:
Multiple indirect pointers are a bit of a brain-twister in "C", as the language isnt much help with the syntax or semantics or run-time behavior of these thingys.

Basically, you want to use pointers as a shortcut to moving or passing large objects.

You want a pointer-to-a-pointer when you want to have a way of modifying the first pointer.  So you pass this pointer-pointer as a parameter, then the function can change the original pointer.

a pointer-to-a-pointer-to-a-pointer comes in handy when you might have to rearrange things im memory, then these p3's give you the extra level of indirection you might need to shuffle things about.

0

Author Commented:
Thanks for the help!I am getting it cleared and will dig more with examples to make me better.
0

## Featured Post

Tackle projects and never again get stuck behind a technical roadblock.