开发者

very basic c question

开发者 https://www.devze.com 2022-12-16 09:57 出处:网络
as we use pointers in the argument list of functions like void f(int *); this means that this func开发者_如何学运维tion will receive a pointer to an integer

as we use pointers in the argument list of functions like

void f(int *); 

this means that this func开发者_如何学运维tion will receive a pointer to an integer

but what does this means

void f(int ***); 

and

void f(int **=0)


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


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.


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)


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


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.


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号