I have the following c function declaration:
float Sum2d( const unsigned int nRows, const unsigned int mCols, float arr[nRows][mCols] )
{
float sumAll = 0;
// I would like to make this change illegal!
arr[0][0] = 15;
for (int i = 0; i < nRows; i++)
for (int j = 0; j < mCols; j++)
sumAll += arr[i][j];
return sumAll;
}
Using the code:
int main()
{
// define a 2d float array
float myArr2d[3][2] = {{1,2}, {3,4}, {5,6}};
// calculate the sum
float sum = Sum2d(3, 2, myArr2d);
// print the sum
printf("%f\n", myOpResult);
// return 1
return 1;
}
This function works well, yet there's one problem: the elements of arr can be altered in the Sum2d() function.
How can I change Sum2d()'s prototype to prevent any changes to arr's elements?
float Sum2d( const unsigned int nRows, const unsigned int mCols, const float arr[nRows][mCols] )file.c:71: warning: passing argument 3 of ‘Sum2d’ from incompatible pointer type file.c:38: note: expected ‘const float (*)[(unsigned int)mCols]’ but argument is of type ‘float (*)[2]’gnucompiler (Ubuntu Linux) inc99mode