如何将数组从函数返回到main()

时间:2016-02-22 09:16:21

标签: c arrays function pointers

main()下面调用getdata(),它会在do while loop中从uart中不断读取字符。 它将它们放在一个数组中,当数组已满时,它将它们写在uart上。

如何才能将数据填充一次并将其返回到main()并将其写入main()?

 int main(void)
 { 
 getdata();

//How can I return the array once to main and write it out over uart()? 

}

void getdata(void)
{
static uint8_t detected = 0;
static uint8_t ndx;
char receiveddata[6];
    char retchar;
do{

    retdata = getch(); //read char from the uart

    if ((retdata == 'Z') && (detected == 0))
    {
        detected = 1;
        ndx = 0;
    }
    if ((detected == 1) && (ndx < 5))
    {
        receiveddata[ndx] = retdata;
        ndx++;
    }
    if (retdata == '\r'){
        receiveddata[ndx] = '\n';
        ndx = 0;
        detected = 0;
        uart_write_buffer(UART_4, (uint8_t *)receiveddata, sizeof(receiveddata));
    }

}while(1);
}

1 个答案:

答案 0 :(得分:0)

你不能从C中的函数返回一个数组。你可以做的是使用指针为这样的数组分配内存:

char *ptr = malloc(6*sizeof(char));

然后将指针返回main。