|
-
July 10th, 2010, 10:50 PM
#1
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?
-
July 10th, 2010, 11:18 PM
#2
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.
-
July 10th, 2010, 11:18 PM
#3
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.
-
July 11th, 2010, 07:08 AM
#4
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.
 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.
-
July 11th, 2010, 08:58 AM
#5
Re: A question regarding char*&
 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*.
Cheers, D Drmmr
Please put [code][/code] tags around your code to preserve indentation and make it more readable.
As long as man ascribes to himself what is merely a posibility, he will not work for the attainment of it. - P. D. Ouspensky
-
July 11th, 2010, 09:09 AM
#6
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*?
 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*.
-
July 11th, 2010, 09:16 AM
#7
Re: A question regarding char*&
 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.
-
July 11th, 2010, 11:34 AM
#8
Re: A question regarding char*&
 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.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|