lpcware

UART sizeof(text) fails, if passed to other function

Discussion created by lpcware Employee on Jun 15, 2016
Latest reply on Jun 15, 2016 by lpcware
Content originally posted in LPCWare by Ano on Fri Aug 29 09:40:12 MST 2014
Hi there, just a quick question. I think it's just my lack of knowledge of C programming here.
In the LPCopen 2.0 example for the UART (for LPC11C24) there is this code snippet:

const char inst1[] = "LPC11xx UART example using ring buffers\r\n";


/* Send initial messages */
Chip_UART_SendRB(LPC_USART, &txring, inst1, sizeof(inst1) - 1);


the sizeof(inst1) here is 42, because there are 42 letters in the message. Ok, that's what I want, too =)
But instead of using this code in one file only, I would like to create a UART.c & UART.h file to handle all the
sending and receiving stuff.
So, this is what I came up with:

in the main function in test.c:
char text1[] = "LPC11xx UART example using ring buffers\r\n";
UART_Send(text1);


in UART.h:
void UART_Send(char text[]);


and in UART.c:

void UART_Send(char text[])
{
Chip_UART_SendRB(LPC_USART, &txring, text, sizeof(text) - 1);
}


It leads to sizeof(text) being 4 in the UART.c file, so this will only transmit 3 characters. So my message is just "LPC".
I could handle this problem by just passing the text and its length to the function like:
char text1[] = "LPC11xx UART example using ring buffers\r\n";
UART_Send(text1, sizeof(text1));

and UART.c:
void UART_Send(char text[], int length)
{
Chip_UART_SendRB(LPC_USART, &txring, text, length - 1);
}


This just works fine, but needs me to take care of the lenght in my main-file. Is there another solution to just pass the text to
the sending function?

Outcomes