C programming strncpy string.h


In C programming the<string.h> strncpy function copies a specific numbers of characters from one array to another.This function is more secure than the ‘strcpy’ function.The declaration of the function is given below.

char *strncpy(char * restrict dst ,
const char * restrict src ,
size_t n);

Parameters:
dst -A pointer to destination array where the string is to be copied.

src -The source array from which the string is to be copied.

n -The number of characters to be copied.

Return type
char* -Returns the string copied to the destination array.

Note if the source and destination memory overlap the behavior is undefined.A simple code example is given below.

Code example

char src[40] = “Calling strncpy function” , dst[40] , *st;

st=strncpy( dst , src , strlen(src)+1); /*strlen function gives the number of characters in the string excluding the null character */

printf(“\ndst=%s”, dst);
printf(“\nst=%s”, st);

Output,

dst=Calling strncpy function
st=Calling strncpy function


 


Some points to note

i)If no null character is included in the first ‘n’ characters

If there is no null character in the first ‘n’ characters to be copied then the string copied will not be null-terminated.In such case the extra memory blocks of the destination may be filled with some garbage value.Consider the program below.

Code example

char src[40] = “Corecplusplustutoril.com rocks!” , dst[40] , *st ;

st=strncpy( dst , src , 20) ; /*Copy only the first 20 characters */

printf(“\ndst=%s”, dst);
printf(“\nst=%s”, st);

Output,

dst=Corecplusplustutoril╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠Corecplusplustutoril.com rocks!
st=Corecplusplustutoril╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠Corecplusplustutoril.com rocks!

After the first 20 characters since there is no null-terminating character the compiler simply fills the remaining memory block with some garbage values,that is why we get the undefined output.To prevent getting from such undefined output we must append the null-terminating character ourself.The correct program is given below.

Code example

char src[40] = “Corecplusplustutoril.com rocks!” , dst[40] , *st ;

st=strncpy( dst , src , 20) ; /*Copy only the first 20 characters */

dst[20]=’\0′; //Append null-terminating character at 20th block(as 21st character)

printf(“\ndst=%s”, dst);
printf(“\nst=%s”, st);

Output,

dst=Corecplusplustutoril
st=Corecplusplustutoril

Now only 20 characters plus the null-terminating character is copied to the ‘dst’ pointed memory.


ii)String copied is lesser than ‘n’ number of characters

If the string copied is lesser than the ‘n’ number of characters specified then the compiler will append the null-terminating character automatically at the end,so in this case you can rest easy.A code example is given below.

Code example

char src[40] = “I love C!” , dst[40] , *st ;

st=strncpy( dst , src , 20) ;

printf(“\ndst=%s”, dst);
printf(“\nst=%s”, st);

Output,

dst=I love C!
st=I love C!


iii)If null-character is found in the middle of the string

If a null-terminating character is found in the middle of the string then the characters after the null-character is ignored.A code example is given below.

Code example

char src1[40] , src = “Null terminating character” , dst[40] , *st ;

for (int i = 0; i < 15; i++)
{
src1[i] = src[i];

if( i==10 )
{
src1[10] = ‘\0’; //assign null character at the 10th memory block(as 11th character)
}
}

st=strncpy( dst , src1 , 20) ;

printf(“\ndst=%s”, dst);
printf(“\nst=%s”, st);

Output,

dst=Null termi
st=Null termi

The number of characters specified for copying is 20 but only 10 characters are copied because the characters after the null character is ignored.


Related links

->C strcpy string.h

->C memmove string.h

->C memcpy string.h