Sam Adamsh
Sam Adamsh

Reputation: 3391

2d array of pointers c++

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

Answers (2)

AusCBloke
AusCBloke

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

Oliver Charlesworth
Oliver Charlesworth

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

Related Questions