I would advice you to declare an array in the main() function itself pass the arrray as reference to getdata function, so whenever getdata() gets updated that will be reflected in the array in main().
Note: By the way why are you using sizeof(recChars), which will give 6, but you're only updating upto 2 location. I've incorporated this also in the code
int main(void)
{
char recChars[6];
int Rxd_Len = 0;
Rxd_Len = getdata(&recChars); //Pass the address of array
uart_write(UART_D, (uint8_t *)recChars, Rxd_Len );
}
int getdata(char *ptr_recChars)
{
uint8_t ndx; // removed static since its not a required/mandatory
char retchar;
ndx = 0;
retchar = getch();
*(ptr_recChars + ndx) = retchar; //Asign the value to the array address + offset
if (retchar == '\r'){
ndx++;
*(ptr_recChars + ndx) = '\n';
}
return ndx;
}
char getch(void) {
uint8_t ch = 0;
int ret;
ret = uart_read(UART_D, &ch);
return ((char) ch);
}
UPDATE:
Is it ok to call uart_write() in main() ?
Yes you can do that, but since the uart_write() will be using the length to transfer you need to keep track of it using another variable.
Also, why is this line necessary? What does it do*(ptr_recChars + ndx) = retchar;?
ptr_recChars is a pointer which points to the array recChars[6], so using this pointer we can update the array recChars[6] even though it is declared in main(), *(ptr_recChars + ndx) , here ndx is 0 so the first element of array is updated, then after that you increment ndx then, using the pointer variable we can point to the new updated location by *(ptr_recChars + ndx) (since ndx is new value now)
Also, I should the & operator in getdata(&recChars);
No, the array name itself is a pointer so for recChars[6] simplay giving recChars alone will give the base address of the array, also note recChars and &recChars[0] means same. So if you give &recChars it means recChars is a address then adding &recChars assumes the address of a address which will give you an invalid value in your expression.
includes, and your own definition ofgetchsuggests you cannot.)mainandgetch, but not forgetdata, whre you instead have a deprecated old-style one?