CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 8 of 8
  1. #1
    Join Date
    Jul 2005
    Posts
    1,030

    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?

  2. #2
    Join Date
    Jan 2006
    Location
    Singapore
    Posts
    6,765

    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.
    C + C++ Compiler: MinGW port of GCC
    Build + Version Control System: SCons + Bazaar

    Look up a C/C++ Reference and learn How To Ask Questions The Smart Way
    Kindly rate my posts if you found them useful

  3. #3
    Join Date
    Feb 2009
    Location
    India
    Posts
    444

    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.
    «_Superman
    I love work. It gives me something to do between weekends.

    Microsoft MVP (Visual C++)

  4. #4
    Join Date
    Jul 2005
    Posts
    1,030

    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 View Post
    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.

  5. #5
    Join Date
    Jul 2005
    Location
    Netherlands
    Posts
    2,042

    Re: A question regarding char*&

    Quote Originally Posted by LarryChen View Post
    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

  6. #6
    Join Date
    Jul 2005
    Posts
    1,030

    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 View Post
    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*.

  7. #7
    Join Date
    Aug 2007
    Posts
    858

    Re: A question regarding char*&

    Quote Originally Posted by LarryChen View Post
    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.

  8. #8
    Join Date
    Oct 2008
    Posts
    1,456

    Re: A question regarding char*&

    Quote Originally Posted by LarryChen View Post
    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
  •  





Click Here to Expand Forum to Full Width

Featured