Qustion about 'const' when passing parameter

I am learning the 'const' when passing parameters in function.
I suppose to put 'const' in different places to check what error I might get. Please give me some advices why it reports error if I move 'const' before the variable '&number'.
If I put 'const' before the variable '&number', it works fine. Why?
Thank you.
//error message
gongzhen@gongzhen-PC ~/Source-3rd/Ch07
$ g++ -o const_declaration const_declaration.cpp
const_declaration.cpp: In function `int main()':
const_declaration.cpp:15: error: invalid initialization of non-const reference o
f type 'int&' from a temporary of type 'int'
const_declaration.cpp:7: error: in passing argument 1 of `char* function1(int&)'

const_declaration.cpp:17: error: invalid initialization of non-const reference o
f type 'int&' from a temporary of type 'int'
const_declaration.cpp:7: error: in passing argument 1 of `char* function1(int&)'

const_declaration.cpp:21: error: invalid initialization of non-const reference o
f type 'int&' from a temporary of type 'int'
const_declaration.cpp:7: error: in passing argument 1 of `char* function1(int&)'

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
#include <iostream>
using namespace std;

//Report error: assignment of readly-only location if adding const before function type
//const char *function1()
char *function1(int &number)
{
	cout<<"number"<<number<<endl;
	return "Some text";
}

int main()
{
	//Report error: assignment of readly-only location if adding const before function type
	function1(3)[1]='a';
	for(int i=0; i<9; i++){
		cout<<""<< function1(4)[i]<<endl;
	}
	//The program could crash if it accidentally tried to alter the 
	//value doing
	function1(5)[1]='a';
	
	return 0;		
}
Not completing following you but the text "Some text" is a constant and stored in the binary executable. If you greped for "Some text" on the executable it would be seen in plan text like:

grep -a Some a.out
H?E?H?E?H?E?H?E?H?E???UH??H??H?}?H?=??!???%?%?%?%?%?%?% ?%"?%$?%&?%(?%*numberSome textL??AS?%??h?????h?????h'?????h??????h#????h????h<????h?????h?????hp????hP????$(@@@?@
$
And this part of you program is read only, you should not be allow to modify that return value.
On my Mac the first return of function1 causes a Bus Error.
Also, the hard-coded numbers of 3, 4 and 5 are constants and the compiler wants the const in the parameter. If you changed the 3,4 and 5 to a variable like int num, no changes would be needed.

1
2
3
4
5
6
int main()
{
    int num(3);
    function1(num);
    return 0;
}

No errors.


closed account (S6k9GNh0)
String laterals are constant and are stored on the executable. Its conceptually impossible to write to that data through a program. But we can do something like:

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
#include <iostream>
#include <cstring>
using namespace std;

//Report error: assignment of readly-only location if adding const before function type
//const char *function1()
char *function1(int number)
{
	cout<<"number"<<number<<endl;
	char* pTmp = new char[11];
        strcpy(pTmp, "Some text\0");
        return pTmp; //Returns writable memory.
}

int main()
{
	char * bob = function1(3);
        bob[1]='a';
	for(int i=0; i<9; i++){
		cout<<""<< bob[i]<<endl;
	}
        delete bob;

	function1(5)[1]='a'; //memory leak
	
	return 0;		
}
Last edited on
Topic archived. No new replies allowed.