Zachary
Zachary

Reputation: 1703

Address of address of array

If I define an variable:
int (**a)[30];
It is pointer. This pointer points to a pointer which points to an array of 30 ints.
How to declare it or initialize it?

int (**a)[10] = new [10][20][30];
int (**a)[10] = && new int[10];  

All doesn't work.

Upvotes: 1

Views: 138

Answers (3)

Dariusz
Dariusz

Reputation: 22311

What do you expect to get by writing &(&var)? This is an equivalent of address of address of a block of memory. Doing things like this just to satisfy the number of * in your code makes no sense.

Think about it - how can you get an address of an address? Even if, by some sheer luck or weird language tricks you manage to do it, there no way it will work.

Upvotes: 0

Stuart Golodetz
Stuart Golodetz

Reputation: 20656

If you want an answer to the question as it stands, then you can do this kind of thing:

int a[30];
int (*b)[30] = &a;
int (**c)[30] = &b;

But it's unlikely to be what you want, as other people have commented. You probably need to clarify your underlying goal - people can only speculate otherwise.


Just to follow on from MooingDuck's remark, I can in fact see a way to do it without the typedef, but not directly:

template <typename T>
T *create(T *const &)
{
    return new T;
}

int (**a)[30] = create(a);

It's not pretty though.

Upvotes: 0

user541686
user541686

Reputation: 210785

The direct answer to your question of how to initialize a (whether or not that's what you actually need) is

int (**a)[10] = new (int (*)[10]);

I don't think this is actually what you want though; you probably want to initialize the pointer to point to an actual array, and either way std::vector is the better way to do it.

Upvotes: 4

Related Questions