codescracker


c

C strncpy()



« Previous Chapter Next Function »


strncpy()

The strncpy() function copies up to count characters from the string pointed to by str2 into the array pointed to by str1. str2 must be a pointer to a null-terminated string.

If str1 and str2 overlap, the behaviour of strncpy() is undefined.

If the string pointed to by str2 has less than count characters, nulls will be appended to the end of str1 until count characters have been copied.

Alternatively, if the string pointed to by str2 is longer than count characters, the resultant array pointed to by str1 will not be null terminated.

The strncpy() function returns a pointer to str1.

strncpy() Syntax

Following is the syntax of the strncpy() function :

#include<string.h>
char *strncpy(char *str1, const char *str2, size_t count);

strncpy() Example

Following code fragment copies at most 79 characters of str1 into str2, thus ensuring that no array boundary overflow occurs.

char str1[128], str2[80];
gets(str1);
strncpy(str2, str1, 79);

« Previous Chapter Next Function »



Tools
Calculator

Quick Links
Signup - Login - Give Online Test