以下程序可以编译为C程序:
#include <stdlib.h>
#include <stdio.h>
void f(int n, int m, int x[n][m]) {
printf("x[0][2] = %i\n",x[0][2]);
}
int main() {
int v[][3] = { {0,1,2}, {3,4,5} };
f(2,3,v);
}
然而,当使用g++编译为C++时,我遇到了以下问题:
main.c:4:29: error: use of parameter outside function body before ‘]’ token
void f(int n, int m, int x[n][m]) {
^
看起来C语言的这个特性在C++中不存在。是否有任何标志可以传递给g++,以便它接受该代码?
std::vector
? - doctorlovevoid f(int n, int m, int (*x)[])
这样的函数声明,在C语言中,你可以在函数内部完成类型转换并补全代码:int (*a)[m] = x;
,但是GCC会报错。 - Eric Postpischil