Reputation: 3391
Why doesn't this code work at making a 2D array pointers in c++? The compiler complains about the second line not being a modifiable l-value.
int* g[2][2];
g[0] = new (int*)[2];
Upvotes: 0
Views: 132
Reputation: 18492
The first line is all you need to create an array of 2 arrays of pointers to int
.
The reason you can't assign a new value to g[0]
is because g[0]
itself is an array, and you can't assign a new value to an array, only its elements.
Upvotes: 3
Reputation: 272467
The type of g[0]
is int* [2]
, i.e. it's an array. You cannot assign to an array.
It's not clear what you're trying to achieve, so I can't offer a solution. If you clarify your question, I may be able to do better.
Upvotes: 5