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
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
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;
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
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
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;
}
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.
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-
ASKER
Thanks for the help!I am getting it cleared and will dig more with examples to make me better.
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.