4

I created this array this array inside a function, with the variable MODEL_VERTEX_NUM initialized @ runtime, which, I guess, is the sticking point here.

loat model_vertices [][]= new float [MODEL_VERTEX_NUM][3];

I got the following errors:

1>.\GLUI_TEMPLATE.cpp(119) : error C2087: 'model_vertices' : missing subscript
1>.\GLUI_TEMPLATE.cpp(119) : error C2440: 'initializing' : cannot convert from 'float (*)[3]' to 'float [][1]'

I realize that when I do:

float model_vertices *[]= new float [MODEL_VERTEX_NUM][3];

The compiler lets this pass, but I wanna understand what's wrong with the previous declaration.

So, what's wrong with the [][] declaration?

asked Nov 14, 2011 at 22:36

1 Answer 1

9

For a two-dimensional array a[X][Y] the compiler needs to know Y to generate code to access the array, so you need to change your code to

float (*model_vertices) [3] = new float [2][3];

If you have an array of type T a[X][Y] and want to access a[x][y] that is equivalent to accessing *(((T*)(&a[0][0])) + x*Y + y). As you can see the compiler needs to know Y but not X to generate code for accessing the array.

answered Nov 14, 2011 at 22:38
Sign up to request clarification or add additional context in comments.

2 Comments

Doing what you say (float model_vertices [] [3]= new float [MODEL_VERTEX_NUM][3];)I got this: error C2440: 'initializing' : cannot convert from 'float (*)[3]' to 'float [][3]'
Sorry, fixed the code now. This compiles: "float (*model_vertices) [3]= new float [2][3];"

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

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.