strncpy strange results

Hi there

I have the following code within my program -

char code[10]; 
char codesent[10];

//code below is located in main loop
      Serial.println(code);
      strncpy (codesent,code);        //Take of copy of the code used for the transaction

I am have a code value in the variable "code" and sometimes I want to take a copy of it into "codesent" so I know what code has been sent out to the PC as the code value will constantly change. Using the following line "strncpy(codesent,code)" gives some very strange results. I would have thought that it should copy code into codesent.

Can anyone confirm if the above should work?

David

With strncpy() you have to specify how many characters to copy.

strncpy (codesent,code,10);

Note, also, that strncpy() doesn't insert the terminating null character.

From the docs;

"Copy a string.

The strncpy() function is similar to strcpy(), except that not more than n bytes of src are copied. Thus, if there is no null byte among the first n bytes of src, the result will not be null-terminated.

[u]In the case where the length of src is less than that of n, the remainder of dest will be padded with nulls[/u]."