Does strncat () interrupt null?

Given this code:

limit = sizeof(str1)-strlen(str1)-1;
strncat(str1,str2,limit);

If the length is str2more than limit, the strncatNul completes str1or do I need to add this code, as in the case strncpy?

str1[sizeof(str1)-1] = '\0'
+5
source share
2 answers

It always completes zero.

Quote C11, chapter §7.24.3.2, (emphasis added)

strncat n ( , , ) , s2, , s1. s2 s1. .

,

, , , s1, strlen(s1)+n+1.

+6

C++ C11 , , .

char str[5];
str="Ami"

char str2[10];
str2="NotGoing"

str 2 , str2 7, - 1. strncat (str, str2,);// .

, str (str2) str,

char str[10];
str="Ami"

char str2[3];

str2="Hello"

str str2 . .

strncat(str,str2,);// case with null termination.

,

, str> = strlen (str) + strlen (str2) +1;

, , - . **

-1

Source: https://habr.com/ru/post/1666790/


All Articles