Zia ur Rahman
Zia ur Rahman

Reputation: 1431

very basic c question

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)

Upvotes: 3

Views: 381

Answers (6)

Andy Brice
Andy Brice

Reputation: 2317

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.

Upvotes: -3

Blindy
Blindy

Reputation: 67352

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).

Upvotes: 15

rook
rook

Reputation: 67019

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.

Upvotes: 1

x4u
x4u

Reputation: 14077

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).

Upvotes: 1

FRotthowe
FRotthowe

Reputation: 3662

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.

Upvotes: 5

missingfaktor
missingfaktor

Reputation: 92016

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)

Upvotes: 4

Related Questions