Thread: Return value string

  1. #1
    the Corvetter
    Join Date
    Sep 2001
    Posts
    1,584

    Return value string

    I was just reading some code (after I realized that a string -- array -- is actually a pointer to the first element in the string) and if had a function:
    Code:
    char* new_function(int x, int y)
    {
        /* Code */
    }
    And I'm not really sure what the 'char*' means. Does it mean that new_function returns a string? If not, what is the return value from new_function that is signified by 'char*'? Thanks.

  2. #2
    Registered User
    Join Date
    Aug 2001
    Posts
    247
    Returns a pointer to char string or character
    hoping to be certified (programming in c)
    here's the news - I'm officially certified.

  3. #3
    the Corvetter
    Join Date
    Sep 2001
    Posts
    1,584
    Code:
    char* rtnString(char string[])
    {
        printf("%s", string);
        string[0] = 'A';
        return (string);
    }
    
    int main()
    {
        char string[] = "New string";
        string = rtnString(string);
        printf("%s", string);
        return (0);
    }
    This code would first print out (from rtnString) "New string" to the screen. Then after going through the function and returning to main, would it print out "Aew string"? I imported the string and then returned it with the return value of 'char *'. Would this work? Thanks.

  4. #4
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    No, that wouldn't work because you're trying to assign a char* to a char. Remember that just the name of the array with nothing else is a pointer to the first element, essentially it's saying array[0]. It's already dereferenced, so you're looking at the value, not the address.

    To properly assign a char* to your array from your function is to create a pointer to the array.

    Code:
    #include <stdio.h>
    
    char* rtnString(char string[])
    {
        printf("\n%s", string); //i added a newline char 
        string[0] = 'A';
        return (string);
    }
    
    int main()
    {
        char string[] = "New string";
        char *s;
        s = string;
        s = rtnString(string); //char* assigned to char* == good :)
        printf("%s", s);
        return (0);
    }
    Don't you just love how pointers work? They make a lot of sense once you get the hang of them, but until then it's just a jumble of *'s and &'s

    -Prelude
    Last edited by Prelude; 09-13-2001 at 05:08 PM.

  5. #5
    the Corvetter
    Join Date
    Sep 2001
    Posts
    1,584
    I don't understand why you had to create pointer to the string in main. After all, a string is a pointer to the first element in the array of characters, right? So, wouldn't the levels of indirection be off? When I'm passing string into rtnString:
    Code:
    string = rtnString(string);
    string is already a pointer, right? So, string would be *(string), which is a pointer. So, wouldn't I be assigning a char * to a char *? Your variable "s" would be a pointer to a pointer, right? Is that level of indirection off? Thanks.

  6. #6
    the Corvetter
    Join Date
    Sep 2001
    Posts
    1,584
    I think I might understand what you are saying.
    Code:
    string = rtnString(string);
    Do you mean that when I say string is equal the return value, that I'm just derefercing string and taking the value? But, aren't I working on the object? So, this is assigning the char * (a string) to a value? I guess that is what I don't understand. How (in my example) is string just a char? Or, is string just the first char (string[0])? Is that what you are saying? So, in my example, string is really just the first character? Not the char *, but just char? I think I'm partially understanding. Thanks...

    Garfield

  7. #7
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    Like I said, pointers are fun, no?

    Okay, here's the long and short of it. Your function is supposed to return a char* or pointer to a char, and that's fine because if you return an entire array then that's what you're passing, so far so good.

    However, when you assign the returned array to itself back in the main, you are actually trying to assign an array to a single element of the same array.

    string = rtnString(string);

    This is pretty much saying, take the array that is given to me by rtnString and assign it to the first element of string. It's like this because when you put in just simply 'string' in an assignment you are really saying, here's the first element, put my value there as opposed to passing it or printing 'string' where it means, this is a pointer to the address of this array. So, to make a long story short:

    string = rtnString(string); //assigning char* to char
    is the same as:
    string[0] = rtnString(string); //also assigning char* to char

    so you get an indirection error, or an LValue error because an array can't be placed into a char. So what I did was create a pointer to your array in main and then assign the char* that you returned from rtnString and placed it there, which was doing the same thing you wanted to do, just without the errors.

    char *s = string;
    s = rtnString(string); //char* to string being assigned the edited
    //string, also of char* == good.

    If you're confused, don't worry. We all went through the whole 'WTF!' phase about pointers

    -Prelude

  8. #8
    the Corvetter
    Join Date
    Sep 2001
    Posts
    1,584
    OOOOHHHH!!!! I understand. string is equal to string[0], which is the first element in the array. The function returns and assigns a pointer (a string) to one element. Thanks! You couldn't have explained it better!

    Garfield

  9. #9
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    phew, and here I thought I was being impossibly cryptic. It's nice to know someone understands my endless rants ;D

    -Prelude

  10. #10
    the Corvetter
    Join Date
    Sep 2001
    Posts
    1,584
    Thanks again! You can't imagine how much you cleared up. You are a life-saver (or, more like a sanity-saver). Thanks!
    1978 Silver Anniversary Corvette

  11. #11
    Unregistered
    Guest
    > No, that wouldn't work because you're trying to assign a char*
    > to a char. Remember that just the name of the array with
    > nothing else is a pointer to the first element, essentially it's
    > saying array[0]. It's already dereferenced, so you're looking at
    > the value, not the address

    No. You are wrong. Well actually, you're right and wrong at the
    same time.

    It is legal to do the following:
    Code:
    char* myfun( char *s ) //duplicate a string
    {
       //this code is irrelevent.
       //pay attention to the actual use of the function
       char *d = NULL;
    
       if ( s == NULL ) return NULL;
       d = malloc ( strlen( s ) + 1 );
       strncpy( d, s, strlen(s) );
       return d;
    }
    
    int main ( void )
    {
       char array[SIZE];
       char *s;
    
       //valid
       s = myfun( "hello" );
       puts( s );
       free( s );
    
       //valid
       strncpy( array, "hi", 2 );
       s = myfun( array );
       puts( s );
       free( s );
    
       //valid, assuming 's' is pointint to usable space
       //note, this would actually cause a memory leak
       //because 's' isn't being freed inside the function,
       //so it'd end up pointing to the new string, "losing"
       //the old string. however, it is still legal
       s = myfun( s );
    
       //invalid.
       //actually, it _is_ valid. You could do this. It however,
       //is wrong. since 'array' is an array, even though it
       //is treated as a pointer, it is wrong in this case because
       //we're actually telling it to point some place else. this
       //is the reason it is wrong.
       array = myfun( s );
    The reason assigning a value to array is incorrect in this place is because we're trying to assign 'array' a new value, rather than use it's currently allocated memory block.

    With an array, you cannot just use 'realloc' or something to give it new space. This is the only real way they differ.

    Quzah.

  12. #12
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    Ah, thank you for clearing that up Quzah. I knew I wasn't getting it completely right and was hoping someone else would pop in and fill in the blanks.

    -Prelude

  13. #13
    the Corvetter
    Join Date
    Sep 2001
    Posts
    1,584
    I have been seeing a lot of code with the function (I guess it's a function) 'malloc'. What is it? What does it do? Thanks.
    1978 Silver Anniversary Corvette

  14. #14
    Mayor of Awesometown Govtcheez's Avatar
    Join Date
    Aug 2001
    Location
    MI
    Posts
    8,823
    http://www.dinkumware.com/htm_cl/stdlib.html#malloc

    Essentially, it allocates memory for things.

  15. #15
    the Corvetter
    Join Date
    Sep 2001
    Posts
    1,584
    Thanks for the site! I was just taking a look over it, and it seems to have extensive knowledge of the many libraries for C. It will be useful in the future. Thanks again!
    1978 Silver Anniversary Corvette

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. C++ ini file reader problems
    By guitarist809 in forum C++ Programming
    Replies: 7
    Last Post: 09-04-2008, 06:02 AM
  2. Code review
    By Elysia in forum C++ Programming
    Replies: 71
    Last Post: 05-13-2008, 09:42 PM
  3. Inheritance Hierarchy for a Package class
    By twickre in forum C++ Programming
    Replies: 7
    Last Post: 12-08-2007, 04:13 PM
  4. Replies: 4
    Last Post: 03-03-2006, 02:11 AM