how to return a string array from a function

后端 未结 9 604
暖寄归人
暖寄归人 2020-12-29 10:53
char * myFunction () {

    char sub_str[10][20]; 
    return sub_str;

} 

void main () {

    char *str;
    str = myFunction();

}

error:return

相关标签:
9条回答
  • 2020-12-29 11:26

    As others have said, you cannot return a local char array to the caller, and have to use heap memory for this.

    However, I would not advise using malloc() within the function.

    Good practice is that, whoever allocates memory, also deallocates it (and handles the error condition if malloc() returns NULL).

    Since your myFunction() does not have control over the memory it allocated once it returned, have the caller provide the memory in which to store the result, and pass a pointer to that memory.

    That way, the caller of your function can de-allocate or re-use the memory (e.g. for subsequent calls to myFunction()) however he sees fit.

    Be careful, though, to either agree on a fixed size for such calls (through a global constant), or to pass the maximum size as additional parameter, lest you end up overwriting buffer limits.

    0 讨论(0)
  • 2020-12-29 11:30

    To programmers just starting out, the concept of a "stack" or the "heap" might be a little confusing, especially if you have started programming in a higher level language like Ruby, Java, Python, etc.

    Consider:

    char **get_me_some_strings() {
      char *ary[] = {"ABC", "BCD", NULL};
      return ary;
    }
    

    The compiler will rightfully issue a complaint about trying to return address of a local variable, and you will most certainly get a segmentation fault trying to use the returned pointer.

    and:

    char **get_me_some_strings() {
      char *ary[] = {"ABC", "BCD", NULL};
      char **strings = ary;
      return strings;
    }
    

    will shut the compiler up, while still getting the same nasty segmentation fault.

    To keep everyone but the zealots happy, you would do something a little more elaborate:

    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    
    char **get_me_some_strings() {
      char *ary[] = { "ABC", "BCD", NULL };
      char **strings = ary; // a pointer to a pointer, for easy iteration
      char **to_be_returned = malloc(sizeof(char*) * 3);
      int i = 0;
      while(*strings) {
        to_be_returned[i] = malloc( sizeof(char) * strlen( *strings ) );
        strcpy( to_be_returned[i++], *strings);
        strings++;
      }
      return to_be_returned;
    }
    

    now use it:

    void i_need_me_some_strings() {
      char **strings = get_me_some_strings();
      while(*strings) {
        printf("a fine string that says: %s", *strings);
        strings++;
      }
    }
    

    Just remember to free the allocated memory when you are done, cuz nobody will do it for you. That goes for all the pointers, not just the pointer to the pointers! (i think).

    To make more sense of it all, you might also want to read this: What and where are the stack and heap?

    0 讨论(0)
  • 2020-12-29 11:31

    first of all You can not return a string variable which is stored in stack you need use malloc to allocate memory dynamicaly here is given datails with the example Go https://nxtspace.blogspot.com/2018/09/return-array-of-string-and-taking-in-c.html get a proper answer

    0 讨论(0)
提交回复
热议问题