void f(char * & pch) {
cout << &pch << " " << pch << endl;
}
int main() {
char *pch2 = new char[11];
strcpy(pch2, "1234567890");
cout << &pch2 << " " << pch2 << endl;
f(pch2);
return 0;
}
gives next output:
0xbfa0d62c 1234567890
0xbfa0d62c 1234567890
but if i would modify first row as follows
void f(char const * const & pch) {
i will get:
0xbfec7df8 1234567890
0xbfec7dfc 1234567890
is difference in pointers appeared because of need to mark new memory cell as const or something else?
pch2is achar*, not achar const*. You cannot bind a reference of typechar const*&to a pointer of typechar*, so the following would be ill-formed:Similarly, if your function was declared as
void f(char const*& pch), you would not be able to call it with achar*argument because of the const-qualifier mismatch.The reason that your example works is that a const reference can bind to a temporary, and the compiler can construct a temporary copy of your
char*pointer, give that temporary the typechar const*, and bind the referencepchto that temporary pointer.