LabWindows/CVI

cancel
Showing results for 
Search instead for 
Did you mean: 

Dubt on strncpy documentation

Hello all, 

 

in the CVI help the documentation of the srtncpy you can read what's following

 

Output
Name Type Description
targetString char [] Contains the target string to which the specified number of characters from the source string are copied. If the source string is shorter than maxChars, ASCII NUL bytes are appended to the contents of this parameter until maxChars are written. If the source string is longer than maxChars, no ASCII NUL bytes are written to the contents of this parameter. If you want to guarantee that an ASCII NUL byte is at the end of the copied bytes, you can use the following:

strncpy (target, source, n);
target[n] = 0;

 

I've put in bold + underline the relevant part.

I've a doubt about the code snippet provided as example

How could it be? I mean, it sounds like an off-by-one to me

 

May I have your kind opinion?

 

Tanks in advance

All the best

0 Kudos
Message 1 of 2
(1,817 Views)

Arrays are zero-based in C. If you want to copy n characters with strncpy you must:

  • Allocate a string of at least n+1 characters
  • Copy n characters (from target[0] to target[n-1])
  • Terminate the string with target[n] = 0

The example in the documentation sounds good to me.



Proud to use LW/CVI from 3.1 on.

My contributions to the Developer Community
________________________________________
If I have helped you, why not giving me a kudos?
0 Kudos
Message 2 of 2
(1,745 Views)