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

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
0
TCSD
Asked:
TCSD
1 Solution
 
Mayank SAssociate 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
 
umangjoshiCommented:
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;


0
 
Mayank SAssociate Director - Product EngineeringCommented:
Yup! Summed up well!
0
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 
gj62Commented:
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
 
jcaldwelCommented:
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
 
grg99Commented:
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
 
TCSDAuthor Commented:
Thanks for the help!I am getting it cleared and will dig more with examples to make me better.
0

Featured Post

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

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