-1

main() below calls getdata() which reads characters continously from the uart in a do while loop. It puts them in an array an when the array is full it writes them out over uart.

How can I get this to just fill the array once and return it to main() and I can write it out in 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);
}
piet.t
  • 11,718
  • 21
  • 43
  • 52
transcend
  • 95
  • 1
  • 1
  • 5

1 Answers1

0

You cannot return an array from a function in C. What you can do is use a pointer to allocate memory for an array like this :

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

and then return the pointer to main.

Marievi
  • 4,951
  • 1
  • 16
  • 33