C - 如何将变量2d数组作为参数传递

时间:2016-04-04 12:15:52

标签: c arrays

我想将2D数组作为参数传递。问题是2d阵列的大小总是变化的。所以我不能写像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()的参数吗?我尝试了很多东西,但似乎没什么用。

2 个答案:

答案 0 :(得分:0)

如果需要数组,则必须使用数组。指针与指针几乎没有关系。将功能更改为:

void drawVertex(const GLfloat vertex[][3], int numberVertex, int shape)

或更好:

void drawVertex(int numberVertex, const GLfloat vertex[numberVertex][3] int shape)

答案 1 :(得分:0)

看起来这里的输入是一个指针const float **vertex的指针,我真的不明白。你能定义为void drawVertex(const float vertex[][3], int numberVertex, int shape)吗?此外,看起来您正在尝试输入一组常量值;但是,输入someArray[][3]未定义为常量数组。