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

cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 

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

806 Views
lpcware
NXP Employee
NXP Employee
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?
Labels (1)
0 Kudos
6 Replies

617 Views
lpcware
NXP Employee
NXP Employee
Content originally posted in LPCWare by starblue on Mon Sep 01 03:22:22 MST 2014

Quote: TheFallGuy
Sizeof(array) will always return 4 as it is the length of a pointer.



That's only true if your array is not an array but a pointer, because it was passed as a parameter.  As the first example shows, <code>sizeof array</code> does indeed give you the size of an array.

Jürgen
0 Kudos

617 Views
lpcware
NXP Employee
NXP Employee
Content originally posted in LPCWare by wmues on Sat Aug 30 14:03:34 MST 2014
void UART_Send(char text[]);


Nobody uses char[] as a parameter.
Please use a char*.

In C, a char[] is synonym to a char * to the start of the array.

I would write:

extern void UART_Send(const char *text);

0 Kudos

617 Views
lpcware
NXP Employee
NXP Employee
Content originally posted in LPCWare by Ano on Fri Aug 29 10:26:34 MST 2014
Sry, I was unclear with my post. Thanks, strlen(string) works just fine. So I'll use it =)
My problem itself is solved. But I would be interested in why the example code uses sizeof() then?
And why does it work when used in this way (this was actually explained in your link), but not when
I try to pass it as an argument (this is what I can't explain)
0 Kudos

617 Views
lpcware
NXP Employee
NXP Employee
Content originally posted in LPCWare by TheFallGuy on Fri Aug 29 10:20:50 MST 2014
Sizeof(array) will always return 4 as it is the length of a pointer. It is calculated at compile time and hard code as it will never change.
Strlen(string) will return the number of characters in an array of characters, excluding the terminating zero. It calculates this at run time.

So, if you want the length of the string use strlen.
0 Kudos

617 Views
lpcware
NXP Employee
NXP Employee
Content originally posted in LPCWare by Ano on Fri Aug 29 10:14:38 MST 2014
Great link, thanks for sharing!
But... yeah, there always is a but ^^
I understand that the first situation (in fist post) is working. But I don't know why the second one (with passing the array) isn't,
or what to change to not have this problem. I think I'm not passing the argument in the correct way, but every other thing I tried
just changed nothing.
0 Kudos

617 Views
lpcware
NXP Employee
NXP Employee
Content originally posted in LPCWare by TheFallGuy on Fri Aug 29 09:45:18 MST 2014
sizeof(type) returns the size of the data type, not the length of the string. You want to use strlen()...

Here is a good explanation of the difference.
http://stackoverflow.com/questions/15177420/what-does-sizeofarray-return/15177499#15177499
0 Kudos