How can I return a character array from a function in C?

后端 未结 6 1763
醉话见心
醉话见心 2020-11-29 10:11

is that even possible? Let\'s say that I want to return an array of two characters

char arr[2];
arr[0] = \'c\';
arr[1] = \'a\';

from a func

相关标签:
6条回答
  • 2020-11-29 10:50

    You can return a pointer for the array from a function, however you can't return pointers to local arrays, the reference will be lost.

    So you have 3 options:

    1. Use a global variable:

      char arr[2];
      
      char * my_func(void){
          arr[0] = 'c';
          arr[1] = 'a';
          return arr;
      }
      
    2. Use dinamic allocation (the caller will have the responsability to free the pointer after using it, make it clear in your documentation)

      char * my_func(void){
      
          char *arr;    
          arr = malloc(2);
          arr[0] = 'c';
          arr[1] = 'a';
      
          return arr;
      }
      
    3. Make the caller allocate the array and use as a reference (my recomendation)

      void my_func(char * arr){
      
          arr[0] = 'c';
          arr[1] = 'a';
      }
      

    if you realy need the function to return the array, you can return the same reference as:

    char * my_func(char * arr){
        arr[0] = 'c';
        arr[1] = 'a';
        return arr;
    }
    
    0 讨论(0)
  • 2020-11-29 10:51
    char* getCharArray()
    {
      return "ca";
    }
    
    0 讨论(0)
  • 2020-11-29 10:53

    This works perfecly:

    int comm_read_data(int file_i2c, unsigned char** buffer)
    {
        *buffer = malloc(BUFFER_SIZE);
        if (i2c_read_bytes(file_i2c, *buffer, BUFFER_SIZE) != 0)
        {
            return -1;
        }
        return BUFFER_SIZE;
    }
    

    And then call the function:

    unsigned char* buffer;
    int length = comm_read_data(file_i2c, &buffer);
    
    /* parse the buffer here */
    
    free(buffer);
    
    0 讨论(0)
  • 2020-11-29 10:54

    You can pass the array to the function and let the function modify it, like this

    void function(char *array)
     {
        array[0] = 'c';
        array[1] = 'a';
     }
    

    and then

    char array[2];
    
    function(array);
    printf("%c%c\n", array[0], array[1]);
    

    If you want it as a return value, you should use dynamic memroy allocation,

    char *function(void)
     {
        char *array;
    
        array = malloc(2);
        if (array == NULL)
            return NULL;
        array[0] = 'c';
        array[1] = 'a';
    
        return array;
     }
    

    then

    char *array = function();
    printf("%c%c\n", array[0], array[1]);
    /* done using `array' so free it because you `malloc'ed it*/
    free(array);
    

    Important Note:

    You should be aware of the fact that the array as filled above is not a string, so you can't for instance do this

    printf("%s\n", array);
    

    because the "%s" expects a matching string to be passed, and in c an array is not a string unless it's last character is '\0', so for a 2 character string you need to allocate space for 3 characters and set the last one to '\0'.

    0 讨论(0)
  • 2020-11-29 10:55

    You've got several options:

    1) Allocate your array on the heap using malloc(), and return a pointer to it. You'll also need to keep track of the length yourself:

    void give_me_some_chars(char **arr, size_t *arr_len)
    {
        /* This function knows the array will be of length 2 */
        char *result = malloc(2);
    
        if (result) {
            result[0] = 'c';
            result[1] = 'a';
        }
    
        /* Set output parameters */
        *arr = result;
        *arr_len = 2;
    }
    
    void test(void)
    {
        char *ar;
        size_t ar_len;
        int i;
    
        give_me_some_chars(&ar, &ar_len);
    
        if (ar) {
            printf("Array:\n");
            for (i=0; i<ar_len; i++) {
                printf(" [%d] = %c\n", i, ar[i]);
            }
            free(ar);
        }
    }
    

    2) Allocate space for the array on the stack of the caller, and let the called function populate it:

    #define ARRAY_LEN(x)    (sizeof(x) / sizeof(x[0]))
    
    /* Returns the number of items populated, or -1 if not enough space */
    int give_me_some_chars(char *arr, int arr_len)
    {
        if (arr_len < 2)
            return -1;
    
        arr[0] = 'c';
        arr[1] = 'a';
    
        return 2;
    }
    
    void test(void)
    {
        char ar[2];
        int num_items;
    
        num_items = give_me_some_chars(ar, ARRAY_LEN(ar));
    
        printf("Array:\n");
        for (i=0; i<num_items; i++) {
            printf(" [%d] = %c\n", i, ar[i]);
        }
    }
    

    DO NOT TRY TO DO THIS

    char* bad_bad_bad_bad(void)
    {
        char result[2];      /* This is allocated on the stack of this function
                                and is no longer valid after this function returns */
    
        result[0] = 'c';
        result[1] = 'a';
    
        return result;    /* BAD! */
    }
    
    void test(void)
    {
        char *arr = bad_bad_bad_bad();
    
        /* arr is an invalid pointer! */
    }
    
    0 讨论(0)
  • 2020-11-29 11:00

    Since you have a predetermined size of you array you can in-fact return the array if you wrap it with a struct:

    struct wrap
    {
        char a[2] ;
    } ;
    
    struct wrap Get( void )
    {
        struct wrap w = { 0 } ;
    
        w.a[0] = 'c';
        w.a[1] = 'a';
    
    return w ;
    }
    
    0 讨论(0)
提交回复
热议问题