Link to home
Start Free TrialLog in
Avatar of TCSD
TCSD

asked on

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...
Thanks in advance.
TCSD
Avatar of Mayank S
Mayank S
Flag of India image

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.
Avatar of umangjoshi
umangjoshi

Please consider the following

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;


Yup! Summed up well!
ASKER CERTIFIED SOLUTION
Avatar of gj62
gj62

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
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;
}
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.

Avatar of TCSD

ASKER

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