How can I return an array of characters from a function in C?

what is even possible? Say I want to return an array of two characters

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

of function. What type am I using for this function? Is my only choice to use pointers and nullify a function? So far, I have been trying to use the char * or char [] function. Obviously, you can only have char (* []) ​​functions. The only reason I want to avoid using pointers is the fact that a function should end when it encounters a “return something”; because the value of "something" is an array of characters (not a string!) that can resize depending on the values ​​that I pass to the function through the main function. Thanks to everyone who answers in advance.

+4
source share
6 answers

You have several options:

1) . Highlight an array on the heap with malloc()and return a pointer to it. You will also need to track 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 caller's stack , and let the called function fill 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]);
    }
}

DON'T TRY TO DO IT

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! */
}
+9
source

You can pass an array to a function and let the function change it, e.g.

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 this return value, you must use memroy dynamic 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 :

, , , , , ,

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

"%s" , , c , '\0', 2 3 '\0'.

0

, , .

, 3 :

  • :

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

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

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

, , :

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

Since you have a predefined size for your array, you can actually return the array if you wrap it in a structure:

struct wrap
{
    char a[2] ;
} ;

struct wrap Get( void )
{
    struct wrap w = { 0 } ;

    w.a[0] = 'c';
    w.a[1] = 'a';

return w ;
}
0
source

This works flawlessly:

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);
-1
source
char* getCharArray()
{
  return "ca";
}
-2
source

All Articles