使用typedef和array时,c中的const问题

时间:2016-05-04 13:36:24

标签: c const typedef

我有以下代码:

typedef float vec3_t[3];

void f(const vec3_t v[2]){
    // do stuff
}

int main(int argc, char * argv[]){
    vec3_t v[2];
    v[2][1] = 1;
    f(v);
    return 0;
}

将无法使用

进行编译
gcc main.c -std=gnu99 -O0 -o main

但是给出了错误

main.c: In function ‘main’:'
main.c:293:5: warning: passing argument 1 of ‘f’ from incompatible pointer type [enabled by default]
     f(v);
     ^
main.c:286:6: note: expected ‘const float (*)[3]’ but argument is of type ‘float (*)[3]’
 void f(const vec3_t v[2]){
      ^

另一方面,如果我删除函数f中的const要求。这一切都运作良好。我无法弄清楚出了什么问题?

1 个答案:

答案 0 :(得分:0)

为什么不将参数转换为const?

typedef float vec3_t[3];

void f(const vec3_t v[2]){
    // do stuff
}

int main(int argc, char * argv[]){
    vec3_t v[2];
    v[2][1] = 1;
    f((const vec3_t*)v);
    return 0;
}