0

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];
5
  • are you trying to allocating memory for g??? Commented Apr 19, 2012 at 0:20
  • no i just want an array of arrays of pointer to int. Commented Apr 19, 2012 at 0:21
  • @SamAdams: You already have that after the first line. What's the intent of the second line? Commented Apr 19, 2012 at 0:22
  • 2
    just trying to learn c++ so i guess the second line is useless and the 2d array is already initialized after the first line. Commented Apr 19, 2012 at 0:25
  • @SamAdams not initialized but rather allocated. Commented Apr 19, 2012 at 0:26

2 Answers 2

5

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.

Sign up to request clarification or add additional context in comments.

Comments

3

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.