?
Solved

Use of double and triple pointer

Posted on 2003-03-10
7
Medium Priority
?
1,995 Views
Last Modified: 2008-01-16
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
Comment
Question by:TCSD
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
7 Comments
 
LVL 30

Expert Comment

by:Mayank S
ID: 8101622
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
 
LVL 1

Expert Comment

by:umangjoshi
ID: 8101642
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
 
LVL 30

Expert Comment

by:Mayank S
ID: 8102220
Yup! Summed up well!
0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 6

Accepted Solution

by:
gj62 earned 750 total points
ID: 8102536
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
 
LVL 1

Expert Comment

by:jcaldwel
ID: 8103503
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
 
LVL 22

Expert Comment

by:grg99
ID: 8106651
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 Comment

by:TCSD
ID: 8107134
Thanks for the help!I am getting it cleared and will dig more with examples to make me better.
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Have you thought about creating an iPhone application (app), but didn't even know where to get started? Here's how: ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ Important pre-programming comments: I’ve never tri…
Summary: This tutorial covers some basics of pointer, pointer arithmetic and function pointer. What is a pointer: A pointer is a variable which holds an address. This address might be address of another variable/address of devices/address of fu…
The goal of this video is to provide viewers with basic examples to understand and use pointers in the C programming language.
The goal of this video is to provide viewers with basic examples to understand and use structures in the C programming language.

777 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question