我正在尝试编写一个可变大小数组并打印它的函数。但我在编译时在函数声明参数列表中有问题。发送可变大小2D数组以在C++中运行
的错误是:
cannot convert ‘int (*)[(((sizetype)(((ssizetype)r) + -1)) + 1)]’ to ‘int**’ for argument ‘1’ to ‘void printHalf(int**, int, int)’
printHalf(b, r, r);
代码:
#include <iostream>
using namespace std;
void printHalf(int **a, int row, int col) { // Problem is here.
for(int i=0; i < row; i++) {
for(int j=0; j < col; j++) {
if(i <= j) {
cout << a[i][j] << " ";
}
}
cout << endl;
}
}
int main() {
int r;
cout << "No. of rows: ";
cin >> r;
int b[r][r];
for(int i=0; i < r; i++) {
for(int j=0; j < r; j++) {
cin >> b[i][j];
}
}
printHalf(b, r, r);
return 0;
}
是什么原因造成这个错误,以及我如何通过各种阵列的功能?
@coincoin我认为你应该在C++中使用'new []'而不是'malloc()'。 – MikeCAT
我认为你应该既不使用也使用'std :: vector'。 – TartanLlama
不要在C++中编写C代码 – ForeverStudent