If this is your first visit, be sure to
check out the FAQ by clicking the
link above. You may have to register or Login
before you can post: click the register link above to proceed. To start viewing messages,
select the forum that you want to visit from the selection below.

A question regarding const

It compiles just fine. But if I change the definition of the function foo a little bit like this,

Code:

void foo(const char* &s)
{
s = "mary";
}

Then I got compiler errors like "cannot convert parameter 1 from 'char *' to 'const char *&'". It looks like it is fine convert char* to const char* but it is not fine to convert char* to const char *&. Why? Thanks.

Re: A question regarding const

In foo, s is a pointer to a const char. The pointer isn't const. What it points to is. Since you're passing by value, s in foo is only in scope for the life of foo. You assign it the the address of the string literal "mary" and it goes out of scope. s in main is unchanged. If you try to modify the string s points to in foo, the compiler won't let you because the char it points to is const.

In the second example, you're trying to pass a reference to a const char*, but s in main is not a const char*, so the compiler is bellyaching.

Somebody with 8 years of C++ experience should be answering questions like that, not asking them.

Re: A question regarding const

Originally Posted by GCDEF

In foo, s is a pointer to a const char. The pointer isn't const. What it points to is. Since you're passing by value, s in foo is only in scope for the life of foo. You assign it the the address of the string literal "mary" and it goes out of scope. s in main is unchanged. If you try to modify the string s points to in foo, the compiler won't let you because the char it points to is const.

In the second example, you're trying to pass a reference to a const char*, but s in main is not a const char*, so the compiler is bellyaching.

Somebody with 8 years of C++ experience should be answering questions like that, not asking them.

I don't quite understand your logic. Based on the same reasoning, in the first example, I am trying to pass const char*, but s in main is not a const char*, so the compiler is bellyaching but actually the compiler is not. I haven't used C++ all the time during the past eight years.

Re: A question regarding const

Originally Posted by LarryChen

I don't quite understand your logic. Based on the same reasoning, in the first example, I am trying to pass const char*, but s in main is not a const char*, so the compiler is bellyaching but actually the compiler is not. I haven't used C++ all the time during the past eight years.

Okay, I'll try again. In the first example, const refers to the char, not to the pointer. So, you're passing a pointer to a const char by value. What this means is a copy of the pointer is passed to foo. Since the pointer isn't const, you can modify its value. Since it's passed by value, the s in foo is a different s than the one in main, and main's s remains unmodified. The char it points to however is const, so you can't modify what it points to.

In the second example, foo is expecting a const char*&, however s is a non const char*, so the type you're passing doesn't match the argument type the function is expecting, which is what the compiler is trying to tell you.

Re: A question regarding const

Originally Posted by GCDEF

Okay, I'll try again. In the first example, const refers to the char, not to the pointer. So, you're passing a pointer to a const char by value. What this means is a copy of the pointer is passed to foo. Since the pointer isn't const, you can modify its value. Since it's passed by value, the s in foo is a different s than the one in main, and main's s remains unmodified. The char it points to however is const, so you can't modify what it points to.

In the second example, foo is expecting a const char*&, however s is a non const char*, so the type you're passing doesn't match the argument type the function is expecting, which is what the compiler is trying to tell you.

I am not concerned about whether s in main is going to be modified or not. I am only concerned about why there is compiler errors in second example? Why would you always bring these two irrelevant aspects together?
Back to your explanation, "In the second example, foo is expecting a const char*&, however s is a non const char*, so the type you're passing doesn't match the argument type the function is expecting". But in the first example, foo is expecting a const char*, however s is a non const char*, so the type I am passing looks like doesn't match the argument type the function is expecting either. Thanks.

Re: A question regarding const

Originally Posted by LarryChen

I am not concerned about whether s in main is going to be modified or not. I am only concerned about why there is compiler errors in second example? Why would you always bring these two irrelevant aspects together?
Back to your explanation, "In the second example, foo is expecting a const char*&, however s is a non const char*, so the type you're passing doesn't match the argument type the function is expecting". But in the first example, foo is expecting a const char*, however s is a non const char*, so the type I am passing looks like doesn't match the argument type the function is expecting either. Thanks.

I don't know how else to put it. In the second example, foo is expecting a reference to a pointer to a const char. s in main is not a pointer to a const char yet you're passing it to a function that expects one, so the compiler is complaining. It's complaining because you're passing it as a reference. In the first example, adding const just means you can't modify what s points to.

Re: A question regarding const

Interesting. I originally tried the posted code on a version of MSVS prior to 2010 and as it compiled cleanly I didn't look too closely at the code. However, it certainly doesn't compile with MSVS2010 for the reasons correctly stated by GCDEF. To make it compile under MSVS2010 one way is make s in main a const char * as in

In your first example, you are passing by value. So a copy of the value of the char * pointer is passed to foo and is assigned to the more restrictive const char * pointer. The s in foo is not the same s as in main. In the second, the foo argument is a reference so no copy of the pointer is made and the s in foo is the same s in main - hence the need for the type of argument passed to foo to be the same as the type of argument expected by foo in its declaration.

All advice is offered in good faith only. You are ultimately responsible for effects of your programs and the integrity of the machines they run on.
C, C++ Compiler: Microsoft VS2015

then the output is garbage as ss is local to function foo. The first case only gives the impression of working properly (but isn't) as the memory corruption is only apparent in the second case. Changing foo again to