Question

I am new to C programming and developing under Ubuntu.

I have some sample code:

char * output1 ="one\0two\0";

char * output2 = "";    
char * first = "one";
char * second = "two";
char * term = "\0";

output2 = malloc(20);
strcat(output2,first);
strcat(output2,term);
strcat(output2,second);
strcat(output2,term);

printf("%s\n",output2);
printf("%s\n",output1);

And the output is the following:

onetwo
one

The output of output1 stops, because there is a \0 in it. But why output2 doesn't stop printing after i added \0 ?

What i am doing wrong and how can i add a \0 to output2 to make its output print

one

Thanks in advance.

Was it helpful?

Solution

Your string "\0" is equivalent a char[]{ 0, 0 }. When you use strcat it overwrites the current null terminator, copies the values up to and including the first '\0', so this is what happens:

// ... are garbage values
strcat(output2,first); // output2 is { 'o', 'n', 'e', '\0', ... } 
strcat(output2,term);  // '\0' is overwritten with `\0` so no change.
strcat(output2,second); // output2 is { 'o', 'n', 'e', 't', 'w', 'o', '\0', ... }   
strcat(output2,term); // '\0' is overwritten with `\0` so no change.

To make this output one:

output2[3] = 0;
// or this maybe easier to read
output2[3] = '\0';

As pointed out in the comments (and other answers):

output2 = malloc(20);
strcat(output2,first);

is undefined behavior since strcat tries to find the first null character. So just set the first character to \0:

output2 = malloc(20);
output2[0] = 0; // or '\0'
strcat(output2,first);

OTHER TIPS

strcat(dest, src) behavior is as follow :

  1. Skip up to first '\0' in dest,
  2. Copy src into dest, overriding former null-character and adding one at the end.

strcat(output2, term) call is being overridden with strcat(outpu2, second) call.

Notice that you have a case of Undefined Behavior (UB) :

Now, when you write output2 = malloc(20), you're not guaranteed that the allocated memory is null-initialized. strcat() will search for a '\0' which can be far after the 20 bytes malloc() allocated.

You should either do

output2 = calloc(20, sizeof(char));
strcat(output2,first);
strcat(output2,term);
strcat(output2,second);
strcat(output2,term);

Or

output2 = malloc(20);
strcpy(output2,first);
strcat(output2,term);
strcat(output2,second);
strcat(output2,term);

Or

output2 = malloc(20);
memset(output2, '\0', 20); // Or output2[0] = '\0';
strcat(output2,first);
strcat(output2,term);
strcat(output2,second);
strcat(output2,term);

With C strings, the \0 character always marks the end of the string. So, the string "" and the string "\0" end up effectively meaning the same thing, even though there's an extra byte in the latter.

That is, strcat(foo, "\0") doesn't append anything onto foo.

the code

output2 = malloc(20);
strcat(output2,first);

allocates 20 random bytes and then you add the string variable first to that

but output2 points to 20 bytes that can contain anything so there could be e.g. "A\0B\0" in it and strcat would then find A\0 and add first to that.

you need to initalize output2

memset(output2,0,20);
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top