A question regarding char*&
Here is the code,
Code:
void foo(char*& s)
{
for(int i=0;i<10;i++)
*s++ = '0'+i;
*s = '\0';
}
int main()
{
char s[100];
foo(s);
return 0;
}
It won't compile and there is a compiler error "cannot convert parameter 1 from 'char [100]' to 'char *&' ". Why?
Re: A question regarding char*&
You do not need a reference parameter in the first place. When you pass a char* by non-const reference, it indicates that you may want to assign to it, and this assignment will be reflected in the caller. In this case, you just want to assign to the objects pointed to, hence you do not need the reference.
If you did want to assign to the char*, then it would not make sense: the char* is converted from an array of char. You cannot assign to an array of char.
Re: A question regarding char*&
A reference to a pointer can only be used if you're passing a pointer.
s is where the actual memory is allocated.
It is different from a pointer.
Re: A question regarding char*&
My understanding is that if I pass a char*&, I mean to pass a char* itself instead of a copy of char*. My main concern here is why there is a compiler error? Thanks.
Quote:
Originally Posted by
laserlight
You do not need a reference parameter in the first place. When you pass a char* by non-const reference, it indicates that you may want to assign to it, and this assignment will be reflected in the caller. In this case, you just want to assign to the objects pointed to, hence you do not need the reference.
If you did want to assign to the char*, then it would not make sense: the char* is converted from an array of char. You cannot assign to an array of char.
Re: A question regarding char*&
Quote:
Originally Posted by
LarryChen
My main concern here is why there is a compiler error? Thanks.
Because you are not passing a char* to the function, you are passing a char array, which is only convertible to a char*, it is not a char*.
Re: A question regarding char*&
I don't quite understand your reasoning. Since a char array might be convertiable to a char*, so why is it a problem that it is not a char*?
Quote:
Originally Posted by
D_Drmmr
Because you are not passing a char* to the function, you are passing a char array, which is only convertible to a char*, it is not a char*.
Re: A question regarding char*&
Quote:
Originally Posted by
LarryChen
I don't quite understand your reasoning. Since a char array might be convertiable to a char*, so why is it a problem that it is not a char*?
Your function needs to take a pointer that it can modify. In the function as you have it written there's no need to take a reference, because you don't actually try to modify the pointer.
Re: A question regarding char*&
Quote:
Originally Posted by
LarryChen
I don't quite understand your reasoning. Since a char array might be convertiable to a char*, so why is it a problem that it is not a char*?
in technical terms, the problem is that the pointer to which the array decays is an r-value and you can't bind a non const reference to an r-value. For example, if you change foo signature to "void foo( char* const&)" then it will compile.