CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Page 1 of 2 12 LastLast
Results 1 to 15 of 20

Thread: return function

  1. #1
    Join Date
    Mar 2009
    Posts
    82

    return function

    Why do I need char *Function()? I mean, why do I need the * in front of the Function1() ??

    Does the * mean pointer?

    Code:
    #include <iostream>
    using namespace std;
    
    char *Function1()
    { 
         return "Some text";
         
    }
    
    int main()
    {
        cout<<Function1();
        
        system("PAUSE");
        return 0;
    }

  2. #2
    Join Date
    Aug 2007
    Posts
    858

    Re: return function

    Quote Originally Posted by StGuru View Post
    Why do I need char *Function()? I mean, why do I need the * in front of the Function1() ??

    Does the * mean pointer?
    Yes. If you only return "char", you are returning a single character. With "char*" you are returning a pointer to the first element in an array of characters, whose end is signified by the character '\0', aka a C-style string.

  3. #3
    VictorN's Avatar
    VictorN is offline Super Moderator Power Poster
    Join Date
    Jan 2003
    Location
    Hanover Germany
    Posts
    20,396

    Re: return function

    1. To make it more convenient to understand I'd rewrite it as:
    Code:
    char* Function1();
    Which just means that the function Function1() returns the value of the char* type (a pointer to the character array)

    1. Your Function1() implementation is wrong: the char array "Some text" goes out of scope just before this function is returning. So it will always return a pointer to the garbage!
    Victor Nijegorodov

  4. #4
    Join Date
    Mar 2009
    Posts
    82

    Re: return function

    Thanks for the replies.
    @Speedo does the pointer points to the first element of the array? The array itself its a pointer, right?

    @VictorN I don't understand why you say my Implementation is wrong?

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

    Re: return function

    Quote Originally Posted by VictorN
    Your Function1() implementation is wrong: the char array "Some text" goes out of scope just before this function is returning. So it will always return a pointer to the garbage!
    I had the impression that since a string literal is returned that is not so due to static storage duration. What would be wrong is the return type, i.e., it should be const char* instead of just char*.

    Quote Originally Posted by StGuru
    does the pointer points to the first element of the array?
    Yes, assuming that VictorN is wrong and the string literal is not destroyed, in which case the pointer returned would be pointing to something that does not exist.

    Quote Originally Posted by StGuru
    The array itself its a pointer, right?
    No, it is an array.
    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

  6. #6
    Lindley is offline Elite Member Power Poster
    Join Date
    Oct 2007
    Location
    Seattle, WA
    Posts
    10,895

    Re: return function

    An array is always convertible to the pointer of the same type, however, so in effect the array "is" a pointer in a sense.

  7. #7
    Join Date
    Mar 2009
    Posts
    82

    Re: return function

    Wait are u saying to me that I return pointer which doesn't exists in the function implementation?

  8. #8
    VictorN's Avatar
    VictorN is offline Super Moderator Power Poster
    Join Date
    Jan 2003
    Location
    Hanover Germany
    Posts
    20,396

    Re: return function

    Quote Originally Posted by laserlight View Post
    I had the impression that since a string literal is returned that is not so due to static storage duration.
    Yes, it is correct.
    However, since the OP wrote this "string literal" as "Some text" I was (and still am) afraid, this function is supposed to return any text somehow generated there...
    Victor Nijegorodov

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

    Re: return function

    Quote Originally Posted by Lindley
    An array is always convertible to the pointer of the same type
    No, an array is always convertible to a pointer to its first element, which certainly is not of the same type as the array

    Quote Originally Posted by StGuru
    Wait are u saying to me that I return pointer which doesn't exists in the function implementation?
    No, what VictorN is saying that by returning a string literal as you do in your Function1() function, the caller (i.e., the main() function) gets a pointer to something that no longer exists since by going out of scope the string literal is destroyed. However, I recall being told differently in the case of string literals, so I am not sure if VictorN is correct. The easiest way out is to simply don't risk it: e.g., return a std::string instead of a pointer.

    EDIT:
    Read VictorN's reply in post #8.
    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

  10. #10
    Join Date
    Mar 2009
    Posts
    82

    Re: return function

    laserlight now I understand what are u saying to me. But I don't find I reason, why the code should not work. It is everything correct. It is true that after calling the function Function1 the pointer will be destroyed. But how that changes something?

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

    Re: return function

    Quote Originally Posted by StGuru
    But I don't find I reason, why the code should not work. It is everything correct. It is true that after calling the function Function1 the pointer will be destroyed. But how that changes something?
    No, the pointer in main() will correctly point to the first character of the string literal. So, in this sense it is much ado about nothing: your code is correct on that point.

    Let's look at an example where it is not correct:
    Code:
    #include <iostream>
    
    const char* function2()
    {
        char text[] = "hello world";
        return text;
    }
    
    int main()
    {
        const char* str = function2();
        std::cout << str << std::endl;
    }
    The problem is that text only exists within function2(). When function2() returns, it is destroyed. Therefore, str would point to something that no longer exists. This is a problem because if it no longer exists, how can you print it? Maybe you do manage to print it, but that would just be "luck".
    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

  12. #12
    Join Date
    Mar 2009
    Posts
    82

    Re: return function

    I got error [Warning] address of local variable `text' returned.
    Anyway, I got the text printed on the screen without altering the code.

    btw why u use const?
    Last edited by StGuru; April 27th, 2009 at 09:58 AM.

  13. #13
    VictorN's Avatar
    VictorN is offline Super Moderator Power Poster
    Join Date
    Jan 2003
    Location
    Hanover Germany
    Posts
    20,396

    Re: return function

    Quote Originally Posted by StGuru View Post
    I got error [Warning] address of local variable `text' returned.
    Anyway, I got the text printed on the screen without altering the code.
    Well, try another example:
    Code:
    char* function3()
    {
        char text[20];
        strcpy(text, "hello world");
        return text;
    }
    Victor Nijegorodov

  14. #14
    Join Date
    Jun 2006
    Location
    M31
    Posts
    885

    Re: return function

    Quote Originally Posted by StGuru View Post
    I got error [Warning] address of local variable `text' returned.
    That's a good thing.

    Quote Originally Posted by StGuru View Post
    Anyway, I got the text printed on the screen without altering the code.
    Pure luck.

    Quote Originally Posted by StGuru View Post
    btw why u use const?
    const-qualification in this context doesn't matter much. The point of the snippet was to demonstrate what happens when you attempt to read/write to something that no longer exists.

    Conversely, const-qualification is very important in your original snippet's context. In fact, implicit convention from const to non-const qualification has been deprecated (for string literals) -- not even mentioning the fact that it's extremely easy to run into undefined behavior by doing this.
    Last edited by Plasmator; April 27th, 2009 at 10:09 AM. Reason: Added clarification.

  15. #15
    Lindley is offline Elite Member Power Poster
    Join Date
    Oct 2007
    Location
    Seattle, WA
    Posts
    10,895

    Re: return function

    Perhaps this will better demonstrate the problem.

    Code:
    #include <iostream>
    
    int function3()
    {
         int vals[] = {42, 23, 64, 1, 0};
         return vals[1];
    }
    
    const char* function2()
    {
        char text[] = "hello world";
        return text;
    }
    
    int main()
    {
        const char* str = function2();
        int val = function3();
        std::cout << str << std::endl;
    }
    The point being that while it was incorrect to access the no-longer-valid str pointer before, there was a good chance it would "work" because nothing was upsetting the state of the stack before it was output. Now there's this other function that tramples all over the stack in between, so the results are less likely to be pretty.

Page 1 of 2 12 LastLast

Tags for this Thread

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