【问题标题】:C - subscripted value is neither array nor pointer nor vectorC - 下标值既不是数组也不是指针也不是向量
【发布时间】:2016-04-10 10:24:52
【问题描述】:

我声明了一个数组并传递了一个二维数组作为参数。但是我不断收到同样的错误:下标值既不是数组也不是指针也不是向量。 对于这一行:vertex[i][j-1] = shape[i][j];

我该如何解决?

头文件:

GLfloat mijnplayer [][4] ={
{0,-0.091057,0.198079,0.084590},
{0,-0.158043,0.158043,0.071039},
{0,-0.071039,0.158043,0.158043}};

函数调用:

int main(void)
{   

drawVertex(mijnplayer,3,1);

}

void drawVertex(GLfloat shape[][4], int numberVertex, int shape)
{
    int i,j;
    GLfloat vertex[][3]={0};


    //convert 4element array to 3 element array
    for(i=0;i<numberVertex;i++)
    {
        for(j=1;j<4;j++)
        {
            vertex[i][j-1] = shape[i][j];
        }

    }

    for(i=0;i<numberVertex;i++)
    {

        glVertex3fv(vertex[i]);
    }

}

编辑:

完整的编译器输出:

cc -c -o mijnTest.o mijnTest.c

mijnTest.c:23:59: 错误:“形状”的类型冲突

void drawVertex(GLfloat shape[][4], int numberVertex, int shape) ^

mijnTest.c:23:25:注意:“形状”的先前定义在这里

void drawVertex(GLfloat shape[][4], int numberVertex, int shape) ^

mijnTest.c:在函数“drawVertex”中:

mijnTest.c:43:26: 错误:下标值既不是数组也不是指针也不是 向量

顶点[i][j-1] = 形状[i][j]; ^

mijnTest.c:在函数“drawScene”中:

mijnTest.c:69:2: 警告:从'drawVertex' 传递参数 1 不兼容的指针类型[默认启用]

drawVertex(assen,6,0); ^

mijnTest.c:23:6:注意:预期为“GLfloat ()[4]”,但参数类型为 ‘GLfloat ()[3]’

void drawVertex(GLfloat shape[][4], int numberVertex, int shape) ^

make: *** [mijnTest.o] 错误 1

【问题讨论】:

  • shape的数据类型是int。
  • 您能否编辑您的问题以包含编译器的完整输出,完整且未经编辑,并包括任何可能的信息说明和警告。
  • @JoachimPileborg,已添加。
  • 阅读错误信息时,始终从顶部开始。在您的情况下,您有mijnTest.c:23:59: error: conflicting types for ‘shape’。这应该会引导您仔细查看函数声明及其参数。
  • 开始,函数有两个不同的参数:drawVertex(),它们都被命名为`shape'

标签: c arrays


【解决方案1】:

这是因为shape 是一个数据类型为int 的变量,并且它既不是数组也不是指针,也不是向量。

要么你的数据类型错误,要么你只是使用了错误的变量。

【讨论】:

  • 我认为没有你提到的。完整代码:dpaste.com/3RN0KR6
  • @J.Doe 你怎么能写 shape[i][j] 同时写 int shape。
  • 天啊!没看到,找了好久!
【解决方案2】:

类似的声明

GLfloat vertex[][3]

仅作为函数参数有效(然后它等于GLfloat (*vertex)[3])。如果您将数组声明为局部或全局变量,您必须给它一个大小。

当您定义mijnplayer 时它会起作用,因为编译器可以从您用于初始化的数据中推断出大小(大小是隐式设置的)。如果你不这样做,那么编译器就无法知道数组的大小,你需要明确地指定它。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2021-07-05
    • 1970-01-01
    • 2013-07-20
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多