c++ - Error: cannot convert 'int (*)[3]' to 'int (*)[100]' for argument '1' to 'void repl(int (*)[100], int, int)' -
hello im trying write program replace each negative number -1 , positive 1 error :
[error] cannot convert 'int ()[3]' 'int ()[100]' argument '1' 'void replace(int (*)[100], int, int)'
what mean ??
#include<iostream> using namespace std; void replace(int arr[][100],int rsize, int csize) { for(int r=0;r<rsize;r++) { (int c=0;c<csize;c++) { if (arr[r][c]>0) arr[r][c]=1; else if (arr[r][c]<0) arr[r][c]=-1; else arr[r][c]=0; } } } int main() { int a[4][3]={ {2,0,-5}, {-8,-9,0}, {0,5,-6}, {1,2,3}}; replace(a,4,3); for(int i=0;i<4;i++) (int j=0;j<3;j++) cout<<a[i][j]<<" ";}cout<<endl; system ("pause"); return 0; }
you declared function void replace(int arr[][100],int rsize, int csize)
- expects 2d array, 'inner' dimension being 100. pass int a[4][3]
has 'inner' dimension 3. compiler can't convert it. dimensions used calculate memory position shift when using arr[x][y]
(it equivalent *(arr + x * 100 + y)
. that's why compiler can't assign array 3 array 100.
if want replace
work dimension change to: void replace(int* arr,int rsize, int csize)
. use *(arr + r*csize + c)
access fields instead of arr[r][c]
even better solution: tagged question c++ - use c++ library :) - std::vector<std::vector<int> >
or std::array
(c++11)
Comments
Post a Comment