我想传递一个二维数组作为参数。问题是二维数组的大小总是变化的。所以我不能写类似 myfunction(int a[2][5]);
我有一个包含大量 3D 坐标的头文件。 例如:
float someArray[][3] = {{0.0,1.0,-2.0},{-1.0,1.0,-2.0},{-1.0,1.0,0.0},{0.0,1.0,0.0},
{-1.0,0.75,-2.0},{-1.0,1.0,-2.0},{-1.0,1.0,0.0},{-1.0,0.75,0.0},
{0.0,0.75,-2.0},{0.0,1.0,-2.0},{0.0,1.0,0.0},{0.0,0.75,0.0},
{0.0,0.75,-2.0},{-1.0,0.75,-2.0},{-1.0,0.75,0.0},{0.0,0.75,0.0},
{0.0,0.75,-2.0},{-1.0,0.75,-2.0},{-1.0,1.0,-2.0},{0.0,1.0,-2.0},
};
void drawVertex(const float **vertex, int numberVertex, int shape)
{
int i;
glPushMatrix();
switch(shape)
{
case 0: glBegin(GL_LINES); break;
case 1: glBegin(GL_POLYGON); break;
default: break;
}
for(i=0;i<numberVertex;i++)
{
glVertex3fv(vertex[i]);
}
glEnd();
glPopMatrix();
}
我尝试过的函数调用:
这些只是崩溃(核心转储)
drawVertex(someArray, 6,1);
drawVertex(*someArray, 6,1);
我应该修改函数drawVertex()的参数吗?我尝试了很多事情,但似乎没有任何效果。
最佳答案
如果需要数组,就必须使用数组。指针到指针与数组关系不大。将函数更改为:
void drawVertex(const GLfloat vertex[][3], int numberVertex, int shape)
或者更好:
void drawVertex(int numberVertex, const GLfloat vertex[numberVertex][3] int shape)
关于C - 如何传递变量二维数组作为参数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36402369/