Functions returning char arrays

This is a discussion on Functions returning char arrays within the C Programming forums, part of the General Programming Boards category; Hi, now i have a most confusing problem. I have a function that returns a string (actually i have a ...

  1. #1
    Registered User
    Join Date
    May 2003
    Posts
    13

    Question Functions returning char arrays

    Hi,
    now i have a most confusing problem. I have a function that returns a string (actually i have a number of them), and i want to store this string in another char array. My first thought was to try saying

    array = function ();

    but of course I found that didn't work just like
    string1 = string2
    won't work. Then I thought of using strcpy :

    strcpy ( array , function() );

    I'm not sure why, but I couldn't make that work either. It occured to me I could try a manual method of copying with a loop, but I couldn't see that working unless I stored function() into a variable first, which is the whole problem! So i thought screw it, no more passing arrays, i'll pass pointers. This worked for some of my functions, but for ones that initialise the array that needs to be returned within them, returning the pointer.. well, you can't return a pointer pointing to a local variable so my compiler tells me.

    So, in conclusion, i'm very stuck. Please help me if you know how to get around this sort of problem!

    Thanks.

  2. #2
    Registered User
    Join Date
    May 2003
    Posts
    161
    You can dynamically allocate the string...

    Code:
    char *function() {
      char *str = malloc(13); // dynamically allocate a char array
    
      if(str == NULL) {
        printf("Error allocating memory for string.");
        exit(1);
      }
    
      strcpy(str, "hello world!");
      return str;
    }
    
    int main(void) {
      char *str = function();
      printf("%s\n", str);
      free(str);                   // free the memory
      return 0;
    }

    edit: added the statement to free the memory

  3. #3
    C++ Developer XSquared's Avatar
    Join Date
    Jun 2002
    Location
    Ontario, Canada
    Posts
    2,718
    Or, you could make it so that the function takes the string to use and the size of the string as parameters.
    Naturally I didn't feel inspired enough to read all the links for you, since I already slaved away for long hours under a blistering sun pressing the search button after typing four whole words! - Quzah

    You. Fetch me my copy of the Wall Street Journal. You two, fight to the death - Stewie

  4. #4
    Been here, done that.
    Join Date
    May 2003
    Posts
    1,161
    It would help if you posted more than the function call. You may be returning just the first character of the array for all we can tell. Post a little more of your code to give us a better reference for the problem, including the definition of the function.
    Definition: Politics -- Latin, from
    poly meaning many and
    tics meaning blood sucking parasites
    -- Tom Smothers

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Manipulating Character arrays in functions.
    By kbro3 in forum C++ Programming
    Replies: 11
    Last Post: 08-16-2008, 03:24 AM
  2. Replies: 12
    Last Post: 08-12-2008, 12:02 AM
  3. Replies: 12
    Last Post: 06-06-2008, 06:26 PM
  4. I'm having a problem with data files.
    By OmniMirror in forum C Programming
    Replies: 4
    Last Post: 05-14-2003, 10:40 PM
  5. returning arrays from functions
    By Leeman_s in forum C++ Programming
    Replies: 11
    Last Post: 06-05-2002, 11:00 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21