What happened to this array declaration?

I created this array of this array inside the function, with the variable MODEL_VERTEX_NUM initialized @runtime, which I think is the braking 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 understand that when I do this:

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

The compiler allows this pass, but I want to understand what is wrong with the previous declaration.

So what happened to the announcement [][] ?

+4
source share
1 answer

For a two-dimensional array a [X] [Y], the compiler must know Y in order 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 you want to access [x] [y], which is equivalent to access (((T) (& a [0] [0])) + x * Y + y). As you can see, the compiler must know Y, but not X, in order to generate code to access the array.

+7
source

Source: https://habr.com/ru/post/1381212/


All Articles