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

后端 未结 6 1782
醉话见心
醉话见心 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: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);
    

提交回复
热议问题