const keyword in function declaration

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
#include <iostream>
#include <iomanip>
using namespace std;
const int NUMCOLS=4;
const int TBLlROWS = 3;
const int TBL2ROWS = 4;
void showArray ( const int array[][NUMCOLS], int );
int main() 
{
	int tablel[TBLlROWS][NUMCOLS] ={{1,2,3,4},
	{5,6,7,8},
	{9,10,11,12}};
	int table2[TBL2ROWS][NUMCOLS] = {{10,20,30,40},
	{52,62,72,82},
	{99,104,117,123}};
	cout <<"The contents of tablel are:"<<"\n";
showArray(tablel, TBLlROWS);
cout << "\nThe contents of table2 are: \n";
showArray(table2, TBL2ROWS);
return 0;
}
void showArray(int const array[][NUMCOLS], int numRows)
{
	for(int row=0;row<numRows;row++)
	{
		for(int col=0;col < NUMCOLS; col++)
		{
			cout << setw(5) << array[row][col] <<" ";
	}
	cout << endl;
}
}

when we use the const keyword with variable and we try to change the value of that variable then the compiler generates the error and if we don't use the const keyword before variable name and we try to change its value then there will be no error. in the above program i never changed the values of the elements of array but when i removed the const keyword in line 7 the compiler generated the error. i never changed the values of the array in program then why the compiler generates the error after removing the const keyword before it?
If you only erased 'const' from line 7, then your function definition (line 22) did not match the function declaration (line 7).

Try removing the const from both lines 7 and 22.
thanks buddy
Topic archived. No new replies allowed.