user65434: I have this code which works :
String ErrorMsg;
.......
if(!UploadPacketError)
{
    ErrorMsg="OK";
}
else
{
   ErrorMsg="NOK";
}

String creates a dynamically allocated array which I want to avoid. I would prefer something like :
unsigned char ErrorMsg[5];
......
if(!UploadPacketError)
    {
        strcpy(ErroMsg,"OK",3); // +1 for the Null Termination ?
    }
    else
    {
       strcpy(ErroMsg,"NOK",4); // +1 for the Null Termination ?
    } 

i.e. is there a way to dynamically assign a value to a fixed size character array ?
Cheers
Thomas

user4143: Almost.
First it's char not unsigned char:
char ErrorMsg[5];`

Second you don't specify a size with strcpy:
strcpy(ErrorMsg, "OK");

There is a variant of strcpy called strncpy which copies at most n characters:
strncpy(ErrorMsg, "OK", 5);

That is used to prevent buffer overruns. But when working with string literals like that it's pretty pointless.

65434: <p>I have this code which works :</p>

<pre><code>String ErrorMsg;
.......
if(!UploadPacketError)
{
    ErrorMsg="OK";
}
else
{
   ErrorMsg="NOK";
}
</code></pre>

<p>String creates a dynamically allocated array which I want to avoid. I would prefer something like :</p>

<pre><code>unsigned char ErrorMsg[5];
......
if(!UploadPacketError)
    {
        strcpy(ErroMsg,"OK",3); // +1 for the Null Termination ?
    }
    else
    {
       strcpy(ErroMsg,"NOK",4); // +1 for the Null Termination ?
    } 
</code></pre>

<p>i.e. is there a way to dynamically assign a value to a fixed size character array ?</p>

<p>Cheers
Thomas</p>
 4143: <p>Almost.</p>

<p>First it's <code>char</code> not <code>unsigned char</code>:</p>

<pre><code>char ErrorMsg[5];`
</code></pre>

<p>Second you don't specify a size with <code>strcpy</code>:</p>

<pre><code>strcpy(ErrorMsg, "OK");
</code></pre>

<p>There is a variant of <code>strcpy</code> called <code>strncpy</code> which copies <em>at most</em> <code>n</code> characters:</p>

<pre><code>strncpy(ErrorMsg, "OK", 5);
</code></pre>

<p>That is used to prevent buffer overruns. But when working with string literals like that it's pretty pointless.</p>
