Alex Rivera | Logout

Initialize pointer to pointer using multiple address operators in C or C++

Asked 2010-09-28T07:45:07.037
10

It just occurred to me That I don't know how to initialize a pointer to pointer from a non pointer value with one statement in C++:

int a = 1;
int** ppa = &&a; //Does not compile
int** ppa = &(&a); //Does not compile
int* pa = &a;   //Works but is a
int** ppa = &pa; //Two step solution

Am I missing something, is the two statement the one and only way to do it?

c++ c
Edit
Report

2 Answers

4

You want a pointer (call it PP) to a pointer (call it P)?

Then P must actually exist. Which means you must declare it.

Of course the compiler will fail at &&a - because you can't take the address of an address. You can take the address of a pointer - but you'll have to declare the pointer first.

Thus:

int a = 1;
int *P = &a;
int **PP = &P;
answered 2010-09-28T07:48:27.093
2

An address is a number. It only has an address of its own if it is held in a variable, i.e. a pointer. So

int *i1 = &a;

makes sense, but

int **i2 = &&a;

makes no sense. However

int **i2 = &i1;

does make sense. Make sense?

answered 2010-09-28T08:05:48.913

Your Answer