tags:

views:

280

answers:

6

as we use pointers in the argument list of functions like

void f(int *); 


this means that this function will receive a pointer to an integer
but what does this means

void f(int ***); 

and

void f(int **=0)
+4  A: 
void f(int ***); 

Here f takes a pointer to pointer to pointer to an int.

void f(int **=0)

This function takes a pointer to pointer to an int as an argument, but this arguments is optional and has a default value of 0 (i.e null)

missingfaktor
+15  A: 
void f(int ***); 

means that the function receives a pointer to a pointer to a pointer to an int. This would work with it:

int x=42;
int *px=&x;
int **ppx=&px;
int ***pppx=&ppx;
f(pppx);

Now about the 2nd one, its a function that receives a pointer to a pointer to an int, and if you give it nothing, it defaults to 0.

int x=42;
int *px=&x;
int **ppx=&px;
f(ppx);  // pt to pt to x
f();     // same as f(0)

UPDATE:

A practical application of this kind of double pointers is a memory allocation routine like:

bool alloc(T **mem, int count);

This function returns true/false depending on whether or not it worked and would update the pointer you pass in with the real memory address, like this:

T *mem;
verify(alloc(&mem, 100));

You pass in an uninitialized pointer and the function can overwrite it with a real value because you passed a pointer to the actual pointer. At the end, mem contains a pointer to valid memory.

Another application, more common but a lot less enlightening, is an array of arrays (so-called jagged arrays).

Blindy
+ 1 for very clear explanation
peakit
yes i understood, but is this not very confusing stuff that we are passing pointer to pointer to pointer to pointer to a function. is it mostly used in programming or not?
Zia ur Rahman
See my update for an example of practical use.
Blindy
The second is a syntax error in C.
Alok
+5  A: 
int *** 

is a pointer to a pointer to a pointer to an int. Think of it as (((int*)*)*).

void f(int **=0)

This function takes a pointer to an int pointer as an argument, but can also be called without arguments in which case the argument will be 0.

FRotthowe
ya i understood, thanks for giving me tip i.e (((int*)*)*).
Zia ur Rahman
+1  A: 
void f(int ***);

here the function argument is a pointer to a pointer to a pointer to an int (or more likely to many of them).

void f(int **=0)  

and here it's just a pointer to a pointer to an int that gets initialized to be 0 (the pointer to the ... is 0, not the int) if the argument is not specified when the function is invoked (optional parameter).

x4u
What C standard is this? I get a syntax error when I compile straight or with -std=c99.
jdizzle
The first line should work with any C compiler but the 2nd is actually not C but C++ and thus works only with a C++ compiler.
x4u
A: 
void f(int ***); 

As the other answers explain, this is a pointer to a pointer to a pointer to an int. It suggest to me that the programmer was not a very good programmer - he (and it was almost certainly a he) was too busy showing off how clever he was with 3 levels of indirection to consider the difficulty of maintaining overly obscure code like this. I've never had to use 3 levels of indirection in approx 20 years of programming in C and C++, and rarely use 2.

Andy Brice
ya i saw this syntax in a question , this becomes very very confusing when we pass arguments to the functions like pointer to pointer to pointer to pointer to integer to the function
Zia ur Rahman
what you say about the usage of such a syntax in c++ programming?
Zia ur Rahman
Sexist, unimaginative and condescending. Would give more than -1 if i could.
Blindy
He's looking for an answer to an academic-type question, not for a code review.
Emile Cormier
+1  A: 

What you are asking about is Multiple Indirection. That page sums up the problem very well, I highly recommend reading that entire page on pointers, it is golden.

Rook