I am trying to write a function which takes variable size array and prints it. But i am having problem in function declaration argument list while compiling.
The error is:
cannot convert ‘int (*)[(((sizetype)(((ssizetype)r) + -1)) + 1)]’ to ‘int**’ for argument ‘1’ to ‘void printHalf(int**, int, int)’
printHalf( b, r, r);
Code:
#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;
}
What is causing this error, and how do i pass various arrays to function?
new[]instead ofmalloc()in C++.std::vectorinstead.