JiaHeng-DLUT
7/28/2019 - 11:13 AM

strncpy()

char * strncpy ( char * destination, const char * source, size_t num );

Copy characters from string Copies the first num characters of source to destination. If the end of the source C string (which is signaled by a null-character) is found before num characters have been copied, destination is padded with zeros until a total of num characters have been written to it.

No null-character is implicitly appended at the end of destination if source is longer than num. Thus, in this case, destination shall not be considered a null terminated C string (reading it as such would overflow).

destination and source shall not overlap (see memmove for a safer alternative when overlapping).

#include <iostream>
#include <string>
using namespace std;

int main() {
	char str1[] = "Hello world!";
	char str2[30] = "";
	cout << strncpy(str2, str1, 7) << endl;	// Hello w
}