简体   繁体   中英

Is null char appended twice at the end of string

I have a question about null char. i have a buffer char buffer_2[245]

memset(buffer_2, 0, 245);

strcpy(buffer_2, "Test");

strcat(buffer_2, "\0");

Do i need the third line? because isn't \\0 autuomatically appended.

Do i need the third line? because isn't \\0 autuomatically appended.

You don't need the third line. strcpy appends the '\\0' .

From man strcpy :

The strcpy() function copies the string pointed to by src, including the terminating null byte ( '\\0' ), to the buffer pointed to by dest. The strings may not overlap, and the destination string dest must be large enough to receive the copy. Beware of buffer overruns! (See BUGS.)

Also note that: Type of "Test" is const char [5] with values 'T' , 'e' , 's' , 't' and '\\0' .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM