Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C++: Does strcat() overwrite or move the null?

Now lets see this small program

char s[20]="One";
strcat(s,"Two");
cout<<s<<endl;

Here at first s has the value "One" and for visual representation this is the value of s:

O - n - e - \0

Then I add "Two" to the end of the string producing this:

O - n - e - T - w - o - \0

Now as you can see the only null in the string at first was after "One" now it is after "OneTwo"

My question is: Is the null overwritten by the string "Two" and then it adds it's own null at the end.

Or is the null that was already there in the beginning moved back to be at the end again?

(This question might seem not to make a difference but I like to know about everything I learn)

Thank you

like image 914
Mohamed Ahmed Nabil Avatar asked Aug 06 '12 19:08

Mohamed Ahmed Nabil


People also ask

Does strcat remove null?

The strcat() function concatenates string2 to string1 and ends the resulting string with the null character. The strcat() function operates on null-ended strings. The string arguments to the function should contain a null character (\0) that marks the end of the string. No length checking is performed.

Does strcat overwrite?

The strcat() function appends the src string to the dest string, overwriting the terminating null byte ('\0') at the end of dest, and then adds a terminating null byte.

Does strcat copy null terminator?

strcat adds characters from the second argument string from to the end of the first argument string to until a terminating-null character is found. The null also is copied.

Does Strncpy null terminate?

In the case of strncpy() , if there is no null character in the first n characters of the source array, the result will not be null-terminated and any remaining characters are truncated.

Does Snprintf null terminate?

snprintf ... Writes the results to a character string buffer. (...) will be terminated with a null character, unless buf_size is zero. So all you have to take care is that you don't pass an zero-size buffer to it, because (obviously) it cannot write a zero to "nowhere".

Does strncat null terminate?

It always null-terminate. The strncat function appends not more than n characters (a null character and characters that follow it are not appended) from the array pointed to by s2 to the end of the string pointed to by s1 . The initial character of s2 overwrites the null character at the end of s1 .


2 Answers

The first \0 is overwritten, and a new \0 is added at the end of the concatenated string. There is no scope for "moving" anything here. These are locations to which values get assigned.

like image 130
juanchopanza Avatar answered Sep 22 '22 16:09

juanchopanza


Although the question has been answered correctly and repeatedly, it may be nice to get a most officialest answer from the source™. Or at least from the sources I can find with Google.

This document, which claims to be the C++ Standard (or a working draft thereof), says:

The C++ Standard library provides 209 standard functions from the C library [including strcat]. --"Standard C library", C.2.7, pg 811

Jumping over to this document claiming to be the C International Standard, we see:

The strcat function appends a copy of the string pointed to by s2 (including the terminating null character) to the end of the string pointed to by s1. The initial character of s2 overwrites the null character at the end of s1. If copying takes place between objects that overlap, the behavior is undefined.

--"The strcat function", 7.21.3.1, pg 327

strcat does indeed overwrite the null character.

like image 34
Kevin Avatar answered Sep 22 '22 16:09

Kevin