<?xml version="1.0" encoding="UTF-8" ?>
<?xml-stylesheet type="text/xsl" href="https://devzone.nordicsemi.com/cfs-file/__key/system/syndication/rss.xsl" media="screen"?><rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:slash="http://purl.org/rss/1.0/modules/slash/" xmlns:wfw="http://wellformedweb.org/CommentAPI/" xmlns:atom="http://www.w3.org/2005/Atom"><channel><title>Creating a string from a character array</title><link>https://devzone.nordicsemi.com/f/nordic-q-a/5174/creating-a-string-from-a-character-array</link><description>Hi all, 
 I would like to use the strcmp - string compare function, however don&amp;#39;t know how to create a string from character array. Standard way would be something like this: 
 char arr[ ] = &amp;quot;Hello World&amp;quot;;
string str(arr);
 
 However the compiler</description><dc:language>en-US</dc:language><generator>Telligent Community 13</generator><lastBuildDate>Mon, 19 Jan 2015 09:16:02 GMT</lastBuildDate><atom:link rel="self" type="application/rss+xml" href="https://devzone.nordicsemi.com/f/nordic-q-a/5174/creating-a-string-from-a-character-array" /><item><title>RE: Creating a string from a character array</title><link>https://devzone.nordicsemi.com/thread/18144?ContentTypeID=1</link><pubDate>Mon, 19 Jan 2015 09:16:02 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:c9f5dd55-7cd5-4542-b80c-b0f0588f733a</guid><dc:creator>Milan</dc:creator><description>&lt;p&gt;Honestly I tried to assign a NULL values into the buffer and it did not work. Now, I have done it properly using memset and it seems to be working fine.&lt;/p&gt;
&lt;pre&gt;&lt;code&gt;memset(p_data, 0, length);
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Thank you.&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: Creating a string from a character array</title><link>https://devzone.nordicsemi.com/thread/18143?ContentTypeID=1</link><pubDate>Fri, 16 Jan 2015 14:51:43 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:161f98fe-7a21-44ca-83f8-22a3fa6a04c6</guid><dc:creator>John</dc:creator><description>&lt;p&gt;Are you making sure to clear the buffer pointed to by p_data after each comparison? If you are reusing the same buffer and not clearing it, that would explain your behavior as the &amp;quot;rate&amp;quot; string is already in the buffer.&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: Creating a string from a character array</title><link>https://devzone.nordicsemi.com/thread/18142?ContentTypeID=1</link><pubDate>Fri, 16 Jan 2015 10:48:25 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:ba2b9a1d-c6ee-413b-b65d-5909458b6f53</guid><dc:creator>Milan</dc:creator><description>&lt;p&gt;Thank you all for contribution I think the strcmp() is clear now however my problem still persists. I will try to explain so hopefully somebody will be able to help.&lt;/p&gt;
&lt;p&gt;I am using the nRF51-dk and in this case original unmodified experimental_ble_app_uart.&lt;/p&gt;
&lt;p&gt;The only change that I have made to the code is the following to trigger LED when string matches:&lt;/p&gt;
&lt;pre&gt;&lt;code&gt;void nus_data_handler(ble_nus_t * p_nus, uint8_t * p_data, uint16_t length)
{
  for (int i = 0; i &amp;lt; length; i++)
  {
      simple_uart_put(p_data[i]);
  }

simple_uart_put(&amp;#39;\n&amp;#39;);

if (strcmp(&amp;quot;rate&amp;quot;, (const char*)p_data) == 0){
  LEDS_INVERT(BSP_LED_1_MASK);
  }
}
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;What I am getting is rather surprising.&lt;/p&gt;
&lt;p&gt;1.When I type in &amp;quot;rat&amp;quot; the LED is not inverted&lt;/p&gt;
&lt;p&gt;2.Then typing in &amp;quot;rate&amp;quot; LED is inverted&lt;/p&gt;
&lt;p&gt;3.Then typing &amp;quot;rat&amp;quot; LED is surprisingly inverted&lt;/p&gt;
&lt;p&gt;4.At this point LED is inverted every time &amp;quot;r&amp;quot; &amp;quot;ra&amp;quot; &amp;quot;rat&amp;quot; &amp;quot;rate&amp;quot; is sent&lt;/p&gt;
&lt;p&gt;5&lt;/p&gt;
&lt;p&gt;5.1. After typing any 4 letters etc. &amp;quot;abcd&amp;quot; LED is not inverted but after that only &amp;quot;rate&amp;quot; can invert LED&lt;/p&gt;
&lt;p&gt;5.2 After typing any 5 letters nothing can invert LED not even &amp;quot;rate&amp;quot;&lt;/p&gt;
&lt;p&gt;Anyone could give an advise how to make this robust?&lt;/p&gt;
&lt;p&gt;Regards&lt;/p&gt;
&lt;p&gt;Milan&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: Creating a string from a character array</title><link>https://devzone.nordicsemi.com/thread/18141?ContentTypeID=1</link><pubDate>Fri, 16 Jan 2015 00:37:32 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:cf181017-83df-497b-a800-77b34171a488</guid><dc:creator>Matt Barr</dc:creator><description>&lt;p&gt;Yes, I wish I had a krone for every time I&amp;#39;ve done that, although in net worth I&amp;#39;d still be well behind Nordic Semiconductor&amp;#39;s CEO! :-)&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: Creating a string from a character array</title><link>https://devzone.nordicsemi.com/thread/18140?ContentTypeID=1</link><pubDate>Thu, 15 Jan 2015 23:25:16 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:8f69227f-5ed1-4c5c-b2e1-63b6f86427d6</guid><dc:creator>John</dc:creator><description>&lt;p&gt;Good point on the *s1 == *s2 in the for loop. I was a bit glib when I looked at the code.&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: Creating a string from a character array</title><link>https://devzone.nordicsemi.com/thread/18139?ContentTypeID=1</link><pubDate>Thu, 15 Jan 2015 22:14:34 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:b95d0ebd-8ce4-4798-9177-9983f2983061</guid><dc:creator>Matt Barr</dc:creator><description>&lt;p&gt;Hi Milan, perhaps your C platform is buggy, or maybe there&amp;#39;s something else going on in your test case, but strcmp should not tell you a string matches an initial sub-string.&lt;/p&gt;
&lt;p&gt;Given a compliant strcmp implementation (as in John&amp;#39;s example, or the standard C library), the code snippet:&lt;/p&gt;
&lt;pre&gt;&lt;code&gt;char buf[BUFSZ];

strcpy (buf, &amp;quot;rat&amp;quot;);
(void) printf (&amp;quot;%s %s rate\n&amp;quot;, buf, (strcmp (buf, &amp;quot;rate&amp;quot;)) ? &amp;quot;!=&amp;quot; : &amp;quot;==&amp;quot;);
strcpy (buf, &amp;quot;rate&amp;quot;);
(void) printf (&amp;quot;%s %s rate\n&amp;quot;, buf, (strcmp (buf, &amp;quot;rate&amp;quot;)) ? &amp;quot;!=&amp;quot; : &amp;quot;==&amp;quot;);
strcpy (buf, &amp;quot;rated&amp;quot;);
(void) printf (&amp;quot;%s %s rate\n&amp;quot;, buf, (strcmp (buf, &amp;quot;rate&amp;quot;)) ? &amp;quot;!=&amp;quot; : &amp;quot;==&amp;quot;);
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;should produce:&lt;/p&gt;
&lt;pre&gt;&lt;code&gt;rat != rate
rate == rate
rated != rate
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;The return 0 in the strcmp example John posted is hit only when *s1 == *s2 and *s1 == 0, so *s2 == 0 as well. The for loop exits on the first difference and returns non-zero, so regardless of operand order that code will return 0 only when the two strings are identical.&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: Creating a string from a character array</title><link>https://devzone.nordicsemi.com/thread/18138?ContentTypeID=1</link><pubDate>Thu, 15 Jan 2015 18:00:31 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:8fa79501-f283-41a1-ae5d-bb0ece25f2dd</guid><dc:creator>Nguyen Hoan Hoang</dc:creator><description>&lt;p&gt;ah! you&amp;#39;re right.  I&amp;#39;ve forgotten about it.&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: Creating a string from a character array</title><link>https://devzone.nordicsemi.com/thread/18137?ContentTypeID=1</link><pubDate>Thu, 15 Jan 2015 17:56:48 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:7cee34a8-1322-452b-a941-de27f4e92575</guid><dc:creator>John</dc:creator><description>&lt;p&gt;Milan may have a point. An implementation of strcmp attributed to PJ Plauger is:&lt;/p&gt;
&lt;pre&gt;&lt;code&gt;int strcmp (const char * s1, const char * s2)
{
    for(; *s1 == *s2; ++s1, ++s2)
        if(*s1 == 0)
            return 0;
    return *(unsigned char *)s1 &amp;lt; *(unsigned char *)s2 ? -1 : 1;
}
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;So if the null terminator of s1 is encountered before a difference is detected with s2, the strcmp will return 0. So, flipping the order of the arguments to strcmp might be one solution.&lt;/p&gt;
&lt;p&gt;The web page I found this on is here: &lt;a href="http://en.wikibooks.org/wiki/C_Programming/C_Reference/string.h/strcmp"&gt;strcmp&lt;/a&gt;&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: Creating a string from a character array</title><link>https://devzone.nordicsemi.com/thread/18136?ContentTypeID=1</link><pubDate>Thu, 15 Jan 2015 16:47:55 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:7687ef9e-8feb-493b-9326-e043d2f802c1</guid><dc:creator>Nguyen Hoan Hoang</dc:creator><description>&lt;p&gt;the &amp;quot;do something&amp;quot; is executed only if the first 4 char of your p_data is equal to &amp;quot;rate&amp;quot;&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: Creating a string from a character array</title><link>https://devzone.nordicsemi.com/thread/18135?ContentTypeID=1</link><pubDate>Thu, 15 Jan 2015 16:39:10 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:01d98211-35d0-4861-9822-6303a4e13ea2</guid><dc:creator>Milan</dc:creator><description>&lt;p&gt;Let say I do this:&lt;/p&gt;
&lt;pre&gt;&lt;code&gt;if (strcmp((char*)p_data, &amp;quot;rate&amp;quot;) == 0)
{do something}
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;The function is executed in case I type all of these: r, ra, rat, rate&lt;/p&gt;
&lt;p&gt;How can I avoid it? I wrote a comparison fuction that works however I expected strcmp to work as well.&lt;/p&gt;
&lt;p&gt;Thanks for your entry.&lt;/p&gt;
&lt;p&gt;Milan&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: Creating a string from a character array</title><link>https://devzone.nordicsemi.com/thread/18134?ContentTypeID=1</link><pubDate>Thu, 15 Jan 2015 16:27:10 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:688165e9-7039-4c88-9a48-e341d9404dea</guid><dc:creator>Nguyen Hoan Hoang</dc:creator><description>&lt;p&gt;type string is a C++ class.  It does not exists in C.  A char array is a string already.&lt;/p&gt;
&lt;p&gt;strcmp(arr, &amp;quot;what ever&amp;quot;) == 0 ... that is how it&amp;#39;s done.  strcmp is a C function.&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item></channel></rss>