<?xml version="1.0" encoding="UTF-8"?>
<rss xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns:taxo="http://purl.org/rss/1.0/modules/taxonomy/" version="2.0">
  <channel>
    <title>topic UART sizeof(text) fails, if passed to other function in LPC Microcontrollers</title>
    <link>https://community.nxp.com/t5/LPC-Microcontrollers/UART-sizeof-text-fails-if-passed-to-other-function/m-p/576633#M19471</link>
    <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;STRONG&gt;Content originally posted in LPCWare by Ano on Fri Aug 29 09:40:12 MST 2014&lt;/STRONG&gt;&lt;BR /&gt;&lt;SPAN&gt;Hi there, just a quick question. I think it's just my lack of knowledge of C programming here.&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;In the LPCopen 2.0 example for the UART (for LPC11C24) there is this code snippet:&lt;/SPAN&gt;&lt;BR /&gt;&lt;TABLE border="1"&gt;&lt;TBODY&gt;&lt;TR&gt;&lt;TD bgcolor="#cacaca"&gt; &lt;PRE&gt;
const char inst1[] = "LPC11xx UART example using ring buffers\r\n";


/* Send initial messages */
Chip_UART_SendRB(LPC_USART, &amp;amp;txring, inst1, sizeof(inst1) - 1);&lt;/PRE&gt; &lt;/TD&gt;&lt;/TR&gt;&lt;/TBODY&gt;&lt;/TABLE&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;the sizeof(inst1) here is 42, because there are 42 letters in the message. Ok, that's what I want, too &lt;SPAN class="lia-unicode-emoji" title=":grinning_face_with_big_eyes:"&gt;&lt;LI-EMOJI id="lia_grinning-face-with-big-eyes" title=":grinning_face_with_big_eyes:"&gt;&lt;/LI-EMOJI&gt;&lt;/SPAN&gt;&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;But instead of using this code in one file only, I would like to create a UART.c &amp;amp; UART.h file to handle all the&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;sending and receiving stuff.&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;So, this is what I came up with:&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;in the main function in test.c:&lt;/SPAN&gt;&lt;BR /&gt;&lt;TABLE border="1"&gt;&lt;TBODY&gt;&lt;TR&gt;&lt;TD bgcolor="#cacaca"&gt; &lt;PRE&gt;char text1[] = "LPC11xx UART example using ring buffers\r\n";
UART_Send(text1);&lt;/PRE&gt; &lt;/TD&gt;&lt;/TR&gt;&lt;/TBODY&gt;&lt;/TABLE&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;in UART.h:&lt;/SPAN&gt;&lt;BR /&gt;&lt;TABLE border="1"&gt;&lt;TBODY&gt;&lt;TR&gt;&lt;TD bgcolor="#cacaca"&gt; &lt;PRE&gt;void UART_Send(char text[]);
&lt;/PRE&gt; &lt;/TD&gt;&lt;/TR&gt;&lt;/TBODY&gt;&lt;/TABLE&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;and in UART.c:&lt;/SPAN&gt;&lt;BR /&gt;&lt;TABLE border="1"&gt;&lt;TBODY&gt;&lt;TR&gt;&lt;TD bgcolor="#cacaca"&gt; &lt;PRE&gt;
void UART_Send(char text[])
{
Chip_UART_SendRB(LPC_USART, &amp;amp;txring, text, sizeof(text) - 1);
}&lt;/PRE&gt; &lt;/TD&gt;&lt;/TR&gt;&lt;/TBODY&gt;&lt;/TABLE&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;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".&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;I could handle this problem by just passing the text and its length to the function like:&lt;/SPAN&gt;&lt;BR /&gt;&lt;TABLE border="1"&gt;&lt;TBODY&gt;&lt;TR&gt;&lt;TD bgcolor="#cacaca"&gt; &lt;PRE&gt;char text1[] = "LPC11xx UART example using ring buffers\r\n";
UART_Send(text1, sizeof(text1));&lt;/PRE&gt; &lt;/TD&gt;&lt;/TR&gt;&lt;/TBODY&gt;&lt;/TABLE&gt;&lt;BR /&gt;&lt;SPAN&gt;and UART.c:&lt;/SPAN&gt;&lt;BR /&gt;&lt;TABLE border="1"&gt;&lt;TBODY&gt;&lt;TR&gt;&lt;TD bgcolor="#cacaca"&gt; &lt;PRE&gt;void UART_Send(char text[], int length)
{
Chip_UART_SendRB(LPC_USART, &amp;amp;txring, text, length - 1);
}&lt;/PRE&gt; &lt;/TD&gt;&lt;/TR&gt;&lt;/TBODY&gt;&lt;/TABLE&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;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&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;the sending function?&lt;/SPAN&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
    <pubDate>Wed, 15 Jun 2016 20:16:16 GMT</pubDate>
    <dc:creator>lpcware</dc:creator>
    <dc:date>2016-06-15T20:16:16Z</dc:date>
    <item>
      <title>UART sizeof(text) fails, if passed to other function</title>
      <link>https://community.nxp.com/t5/LPC-Microcontrollers/UART-sizeof-text-fails-if-passed-to-other-function/m-p/576633#M19471</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;STRONG&gt;Content originally posted in LPCWare by Ano on Fri Aug 29 09:40:12 MST 2014&lt;/STRONG&gt;&lt;BR /&gt;&lt;SPAN&gt;Hi there, just a quick question. I think it's just my lack of knowledge of C programming here.&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;In the LPCopen 2.0 example for the UART (for LPC11C24) there is this code snippet:&lt;/SPAN&gt;&lt;BR /&gt;&lt;TABLE border="1"&gt;&lt;TBODY&gt;&lt;TR&gt;&lt;TD bgcolor="#cacaca"&gt; &lt;PRE&gt;
const char inst1[] = "LPC11xx UART example using ring buffers\r\n";


/* Send initial messages */
Chip_UART_SendRB(LPC_USART, &amp;amp;txring, inst1, sizeof(inst1) - 1);&lt;/PRE&gt; &lt;/TD&gt;&lt;/TR&gt;&lt;/TBODY&gt;&lt;/TABLE&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;the sizeof(inst1) here is 42, because there are 42 letters in the message. Ok, that's what I want, too &lt;SPAN class="lia-unicode-emoji" title=":grinning_face_with_big_eyes:"&gt;&lt;LI-EMOJI id="lia_grinning-face-with-big-eyes" title=":grinning_face_with_big_eyes:"&gt;&lt;/LI-EMOJI&gt;&lt;/SPAN&gt;&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;But instead of using this code in one file only, I would like to create a UART.c &amp;amp; UART.h file to handle all the&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;sending and receiving stuff.&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;So, this is what I came up with:&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;in the main function in test.c:&lt;/SPAN&gt;&lt;BR /&gt;&lt;TABLE border="1"&gt;&lt;TBODY&gt;&lt;TR&gt;&lt;TD bgcolor="#cacaca"&gt; &lt;PRE&gt;char text1[] = "LPC11xx UART example using ring buffers\r\n";
UART_Send(text1);&lt;/PRE&gt; &lt;/TD&gt;&lt;/TR&gt;&lt;/TBODY&gt;&lt;/TABLE&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;in UART.h:&lt;/SPAN&gt;&lt;BR /&gt;&lt;TABLE border="1"&gt;&lt;TBODY&gt;&lt;TR&gt;&lt;TD bgcolor="#cacaca"&gt; &lt;PRE&gt;void UART_Send(char text[]);
&lt;/PRE&gt; &lt;/TD&gt;&lt;/TR&gt;&lt;/TBODY&gt;&lt;/TABLE&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;and in UART.c:&lt;/SPAN&gt;&lt;BR /&gt;&lt;TABLE border="1"&gt;&lt;TBODY&gt;&lt;TR&gt;&lt;TD bgcolor="#cacaca"&gt; &lt;PRE&gt;
void UART_Send(char text[])
{
Chip_UART_SendRB(LPC_USART, &amp;amp;txring, text, sizeof(text) - 1);
}&lt;/PRE&gt; &lt;/TD&gt;&lt;/TR&gt;&lt;/TBODY&gt;&lt;/TABLE&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;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".&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;I could handle this problem by just passing the text and its length to the function like:&lt;/SPAN&gt;&lt;BR /&gt;&lt;TABLE border="1"&gt;&lt;TBODY&gt;&lt;TR&gt;&lt;TD bgcolor="#cacaca"&gt; &lt;PRE&gt;char text1[] = "LPC11xx UART example using ring buffers\r\n";
UART_Send(text1, sizeof(text1));&lt;/PRE&gt; &lt;/TD&gt;&lt;/TR&gt;&lt;/TBODY&gt;&lt;/TABLE&gt;&lt;BR /&gt;&lt;SPAN&gt;and UART.c:&lt;/SPAN&gt;&lt;BR /&gt;&lt;TABLE border="1"&gt;&lt;TBODY&gt;&lt;TR&gt;&lt;TD bgcolor="#cacaca"&gt; &lt;PRE&gt;void UART_Send(char text[], int length)
{
Chip_UART_SendRB(LPC_USART, &amp;amp;txring, text, length - 1);
}&lt;/PRE&gt; &lt;/TD&gt;&lt;/TR&gt;&lt;/TBODY&gt;&lt;/TABLE&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;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&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;the sending function?&lt;/SPAN&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Wed, 15 Jun 2016 20:16:16 GMT</pubDate>
      <guid>https://community.nxp.com/t5/LPC-Microcontrollers/UART-sizeof-text-fails-if-passed-to-other-function/m-p/576633#M19471</guid>
      <dc:creator>lpcware</dc:creator>
      <dc:date>2016-06-15T20:16:16Z</dc:date>
    </item>
    <item>
      <title>Re: UART sizeof(text) fails, if passed to other function</title>
      <link>https://community.nxp.com/t5/LPC-Microcontrollers/UART-sizeof-text-fails-if-passed-to-other-function/m-p/576634#M19472</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;STRONG&gt;Content originally posted in LPCWare by TheFallGuy on Fri Aug 29 09:45:18 MST 2014&lt;/STRONG&gt;&lt;BR /&gt;&lt;SPAN&gt;sizeof(type) returns the size of the data type, not the length of the string. You want to use strlen()...&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;Here is a good explanation of the difference.&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;&lt;A class="jive-link-external-small" href="https://community.nxp.com/external-link.jspa?url=http%3A%2F%2Fstackoverflow.com%2Fquestions%2F15177420%2Fwhat-does-sizeofarray-return%2F15177499%2315177499" rel="nofollow" target="_blank"&gt;http://stackoverflow.com/questions/15177420/what-does-sizeofarray-return/15177499#15177499&lt;/A&gt;&lt;/SPAN&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Wed, 15 Jun 2016 20:16:18 GMT</pubDate>
      <guid>https://community.nxp.com/t5/LPC-Microcontrollers/UART-sizeof-text-fails-if-passed-to-other-function/m-p/576634#M19472</guid>
      <dc:creator>lpcware</dc:creator>
      <dc:date>2016-06-15T20:16:18Z</dc:date>
    </item>
    <item>
      <title>Re: UART sizeof(text) fails, if passed to other function</title>
      <link>https://community.nxp.com/t5/LPC-Microcontrollers/UART-sizeof-text-fails-if-passed-to-other-function/m-p/576635#M19473</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;STRONG&gt;Content originally posted in LPCWare by Ano on Fri Aug 29 10:14:38 MST 2014&lt;/STRONG&gt;&lt;BR /&gt;&lt;SPAN&gt;Great link, thanks for sharing!&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;But... yeah, there always is a but ^^&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;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,&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;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&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;just changed nothing.&lt;/SPAN&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Wed, 15 Jun 2016 20:16:18 GMT</pubDate>
      <guid>https://community.nxp.com/t5/LPC-Microcontrollers/UART-sizeof-text-fails-if-passed-to-other-function/m-p/576635#M19473</guid>
      <dc:creator>lpcware</dc:creator>
      <dc:date>2016-06-15T20:16:18Z</dc:date>
    </item>
    <item>
      <title>Re: UART sizeof(text) fails, if passed to other function</title>
      <link>https://community.nxp.com/t5/LPC-Microcontrollers/UART-sizeof-text-fails-if-passed-to-other-function/m-p/576636#M19474</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;STRONG&gt;Content originally posted in LPCWare by TheFallGuy on Fri Aug 29 10:20:50 MST 2014&lt;/STRONG&gt;&lt;BR /&gt;&lt;SPAN&gt;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.&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;Strlen(string) will return the number of characters in an array of characters, excluding the terminating zero. It calculates this at run time.&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;So, if you want the length of the string use strlen.&lt;/SPAN&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Wed, 15 Jun 2016 20:16:19 GMT</pubDate>
      <guid>https://community.nxp.com/t5/LPC-Microcontrollers/UART-sizeof-text-fails-if-passed-to-other-function/m-p/576636#M19474</guid>
      <dc:creator>lpcware</dc:creator>
      <dc:date>2016-06-15T20:16:19Z</dc:date>
    </item>
    <item>
      <title>Re: UART sizeof(text) fails, if passed to other function</title>
      <link>https://community.nxp.com/t5/LPC-Microcontrollers/UART-sizeof-text-fails-if-passed-to-other-function/m-p/576637#M19475</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;STRONG&gt;Content originally posted in LPCWare by Ano on Fri Aug 29 10:26:34 MST 2014&lt;/STRONG&gt;&lt;BR /&gt;&lt;SPAN&gt;Sry, I was unclear with my post. Thanks, strlen(string) works just fine. So I'll use it =) &lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;My problem itself is solved. But I would be interested in why the example code uses sizeof() then?&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;And why does it work when used in this way (this was actually explained in your link), but not when&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;I try to pass it as an argument (this is what I can't explain) &lt;/SPAN&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Wed, 15 Jun 2016 20:16:19 GMT</pubDate>
      <guid>https://community.nxp.com/t5/LPC-Microcontrollers/UART-sizeof-text-fails-if-passed-to-other-function/m-p/576637#M19475</guid>
      <dc:creator>lpcware</dc:creator>
      <dc:date>2016-06-15T20:16:19Z</dc:date>
    </item>
    <item>
      <title>Re: UART sizeof(text) fails, if passed to other function</title>
      <link>https://community.nxp.com/t5/LPC-Microcontrollers/UART-sizeof-text-fails-if-passed-to-other-function/m-p/576638#M19476</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;STRONG&gt;Content originally posted in LPCWare by wmues on Sat Aug 30 14:03:34 MST 2014&lt;/STRONG&gt;&lt;BR /&gt;&lt;TABLE border="1"&gt;&lt;TBODY&gt;&lt;TR&gt;&lt;TD bgcolor="#cacaca"&gt; &lt;PRE&gt;void UART_Send(char text[]);&lt;/PRE&gt; &lt;/TD&gt;&lt;/TR&gt;&lt;/TBODY&gt;&lt;/TABLE&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;Nobody uses char[] as a parameter.&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;Please use a char*.&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;In C, a char[] is synonym to a char * to the start of the array.&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;I would write:&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;extern void UART_Send(const char *text);&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Wed, 15 Jun 2016 20:16:20 GMT</pubDate>
      <guid>https://community.nxp.com/t5/LPC-Microcontrollers/UART-sizeof-text-fails-if-passed-to-other-function/m-p/576638#M19476</guid>
      <dc:creator>lpcware</dc:creator>
      <dc:date>2016-06-15T20:16:20Z</dc:date>
    </item>
    <item>
      <title>Re: UART sizeof(text) fails, if passed to other function</title>
      <link>https://community.nxp.com/t5/LPC-Microcontrollers/UART-sizeof-text-fails-if-passed-to-other-function/m-p/576639#M19477</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;STRONG&gt;Content originally posted in LPCWare by starblue on Mon Sep 01 03:22:22 MST 2014&lt;/STRONG&gt;&lt;BR /&gt;&lt;HR /&gt;&lt;SPAN style="color: #0000ff;"&gt;&lt;STRONG&gt;Quote: TheFallGuy&lt;/STRONG&gt;&lt;BR /&gt;Sizeof(array) will always return 4 as it is the length of a pointer.&lt;/SPAN&gt;&lt;HR /&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;That's only true if your array is not an array but a pointer, because it was passed as a parameter.&amp;nbsp; As the first example shows, &amp;lt;code&amp;gt;sizeof array&amp;lt;/code&amp;gt; does indeed give you the size of an array.&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;Jürgen&lt;/SPAN&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Wed, 15 Jun 2016 20:16:20 GMT</pubDate>
      <guid>https://community.nxp.com/t5/LPC-Microcontrollers/UART-sizeof-text-fails-if-passed-to-other-function/m-p/576639#M19477</guid>
      <dc:creator>lpcware</dc:creator>
      <dc:date>2016-06-15T20:16:20Z</dc:date>
    </item>
  </channel>
</rss>

