returning a string in C

Discussion in 'Programmer's Corner' started by MonsterofAmp, Jul 13, 2009.

  1. MonsterofAmp

    Thread Starter Member

    Mar 14, 2009
    11
    0
    Hi, i have a function i made that stores the string you type into an array the function then returns that array. Now the problem is i have a file stream that needs a file name,
    fnameptr(points to fnamearray[]) = keyboardfunc(); how would i get this to work if its even possible?
    the keyboardfunc() returns the array of what you typed (the array has a max thats definable, both the fnameptr and array returned by keyboardfunc() have the same size array).
    I've tried multiples things to no avail.
    Any help or any explanations would be appreciated. If i didnt explain something well enough or you need more info about the code please let me know.
     
  2. Papabravo

    Expert

    Feb 24, 2006
    10,178
    1,799
    Well you don't actually return a string. What you can pass into or out of a function is a pointer to a string. There is a caveat and that is you shouldn't return a pointer to a string which is local to the function, since the memory is deallocated when the function returns.

    In your case you can have one or more arrays defined outside of any function which can be used to hold filenames or text or whatever you want.
     
  3. Mark44

    Well-Known Member

    Nov 26, 2007
    626
    1
    I'm guessing that the prototype for your function is char * keyboardfunc();

    To elaborate on what Papabravo said, your function is returning a pointer to a string; that is, the address of the first character in the string.

    A string is a fundamentally different data type from scalar types such as int, float, char, and the others. It is for this reason that the C standard library has a whole set of functions for copying strings (e.g., strcpy()), comparing strings (strcmp()), and other operations on strings.

    If you want to "return" an input string, you'll need to do this by using a parameter to your function, using an array whose storage is allocated by the caller of your function.
     
  4. MonsterofAmp

    Thread Starter Member

    Mar 14, 2009
    11
    0
    Hi, thank you for the replies, i still don't quite understand what you are saying but is it something like this...
    char arr[6];
    char keyhandle(array[]){
    retrun array;
    }
    This code still does not quite work, doyou have any good website/tut's that teach pointer/array and functions, the ones i've found don't quite make sense.
     
  5. Mark44

    Well-Known Member

    Nov 26, 2007
    626
    1
    Your function keyhandle() now returns a single char, not an entire array of chars as you want it to do.

    The bottom line is that you can't return the contents of an array using a return statement. You can return a scalar variable (like a single char, or an int, or float, or double, or a pointer), but you can't return a string (or other array types).
     
  6. MonsterofAmp

    Thread Starter Member

    Mar 14, 2009
    11
    0
    O.k. Thank you for clearring that up, i worked around it and made an array and pointer outside of the function, then passed the pointer into the function allowing me to alter it. It works now thanks abunch guys!
     
  7. Papabravo

    Expert

    Feb 24, 2006
    10,178
    1,799
    You're very welcome. I'd like to add that you appear to be both more resourceful and more receptive to suggestions that lead you to the solution. I have always found that process much more satisfying than having an answer handed to me.
     
Loading...