简体   繁体   English

C-下标值既不是数组,也不是指针,也不是向量

[英]C - subscripted value is neither array nor pointer nor vector

I declared an array and pass a 2D array as a parameter. 我声明了一个数组并将2D数组作为参数传递。 However I keep getting the same error: subscripted value is neither array nor pointer nor vector. 但是我仍然遇到相同的错误:下标值既不是数组,也不是指针,也不是矢量。 for this line: vertex[i][j-1] = shape[i][j]; 对于这条线:vertex [i] [j-1] = shape [i] [j];

How can I solve it? 我该如何解决?

header file: 头文件:

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

function call: 函数调用:

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]);
    }

}

EDIT: 编辑:

the complete compiler output: 完整的编译器输出:

cc -c -o mijnTest.o mijnTest.c cc -c -o mijnTest.o mijnTest.c

mijnTest.c:23:59: error: conflicting types for 'shape' mijnTest.c:23:59:错误:“形状”的类型冲突

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

mijnTest.c:23:25: note: previous definition of 'shape' was here mijnTest.c:23:25:注意:先前对“形状”的定义在这里

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

mijnTest.c: In function 'drawVertex': mijnTest.c:在函数'drawVertex'中:

mijnTest.c:43:26: error: subscripted value is neither array nor pointer nor vector mijnTest.c:43:26:错误:下标值既不是数组也不是指针也不是矢量

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

mijnTest.c: In function 'drawScene': mijnTest.c:在函数'drawScene'中:

mijnTest.c:69:2: warning: passing argument 1 of 'drawVertex' from incompatible pointer type [enabled by default] mijnTest.c:69:2:警告:从不兼容的指针类型传递“ drawVertex”的参数1 [默认启用]

drawVertex(assen,6,0); drawVertex(assen,6,0); ^ ^

mijnTest.c:23:6: note: expected 'GLfloat ( )[4]' but argument is of type 'GLfloat ( )[3]' mijnTest.c:23:6:注意:预期为'GLfloat( )[4]',但参数的类型为'GLfloat( )[3]'

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

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

A declaration like 像这样的声明

GLfloat vertex[][3]

is only valid as a function argument (and then it's equal to GLfloat (*vertex)[3] ). 仅作为函数参数有效(然后等于GLfloat (*vertex)[3] )。 If you declare an array as a local or global variable, you must give it a size. 如果将数组声明为局部变量或全局变量,则必须为其指定大小。

It works when you define mijnplayer because then the compiler can deduce the size from the data you use for initialization (the size is set implicitly). 它在定义mijnplayermijnplayer因为编译器可以从用于初始化的数据中推断出大小(大小是隐式设置的)。 If you don't do it then the compiler can't know what size the array is, and you need to specify it explicitly. 如果不这样做,则编译器将无法知道数组的大小,因此需要明确指定它。

Its because shape is an variable of data type int and its neither array nor pointer nor vector. 这是因为shape是数据类型int的变量, 它既不是数组,也不是指针,也不是向量。

Either you got the data type wrong or you are just using the wrong variable. 要么数据类型错误,要么使用的变量错误。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM