0

I implemented strcat function and when just playing around i used my function and started comparing with the standard string function strcat but i found an issue regarding the output of my_strcat and strcat. In my_strcat "programming" is printed twice while in strcat it is just printed once which is the expected behavior, but in case of my_strcat why is "programming displayed twice".

Things i tried: After while loop i put *dest++ = '\0' even then "programming" gets displayed twice. Am using linux system for programming and gcc.

o/p:
strcat:Pl_z� programming 
my_strcat:Pl_z� programming  programming 
char *my_strcat(char *dest, const char *src)   {
    char *dst = dest;
    while(*dest!= '\0')
        dest++;
    while(*dest++ = *src++) 
        ;  
    return dst;
}

int main(int argc, char **argv) {
    // char string1[25] = "C";
    char string1[25];
    char string2[] = " programming " ;
    printf(" strcat:%s\n",strcat(string1, string2));
    printf(" my_strcat:%s\n",my_strcat(string1, string2));   
}  
Iharob Al Asimi
  • 52,653
  • 6
  • 59
  • 97
Manu
  • 5,534
  • 6
  • 32
  • 42
  • `strcat()` is not meant to display anything. – Iharob Al Asimi Mar 01 '15 at 15:20
  • 2
    Your code exhibits Undefined Behavior as `string1` isn't initialized. As for two "programming" strings being displayed, the first `strcat` appended the first "programming" string and your `strcat` appended the second one. – Spikatrix Mar 01 '15 at 15:30

3 Answers3

3

The issue is not with your implementation of strcat. You are forgetting to clear string1 in between the calls, so my_strcat (correctly) appends to it!

If you want to "clear" it (for the purposes of strcat) you can simply set the first character to '\0':

 string1[0] = '\0';

Thanks to iharob for helping me recover from my colossal brainfart.


The reason this is happening (like sth said, it's expected behavior) is that the purpose of strcat is to append a string at the end of another. Quoting the manual page:

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.


As Ôrel points out below, the behavior of your program is undefined to begin with as you are not initializing string1 before using it. See the answers to this question for more information.

Community
  • 1
  • 1
Martin Törnwall
  • 9,299
  • 2
  • 28
  • 35
  • 1
    `string1[0] = '\0'` will be sufficient. – Iharob Al Asimi Mar 01 '15 at 15:22
  • the same variables string1 and string2 am passing to strcat function and it gives me the correct output...so does strcat internally use memeset ? – Manu Mar 01 '15 at 15:24
  • @Manu, no it should also fail and give the same problem, although this is not the cause of your observed output. – Iharob Al Asimi Mar 01 '15 at 15:25
  • @Manu to clarify, the results would be the same if you switched the order of the calls to `strcat` and `my_strcat`. – Martin Törnwall Mar 01 '15 at 15:26
  • yes you are correct just hurried to a conclusion my bad, thank you – Manu Mar 01 '15 at 15:28
  • 2
    string1 is no initialized even for first call, you can have strange behaviour. `string1[0] = '\0'` should be done even before sctrcat – Ôrel Mar 01 '15 at 15:28
  • Ôrel: indeed he gets strange behaviour: `Pl_z�`. And given the buffer overflow, I'm surprised he does not get worse consequences of undefined behaviour. – chqrlie Mar 01 '15 at 15:47
2

As others have mentioned your program is behaving correctly although it has undefined behavior because you don't initialize the string1 array so

while (*dest++ != '\0')

will never find the '\0' or perhaps it will, you can't be sure, so you need to set it's first element to '\0' for your code to work.

Your my_strcat() implementation is correct, but to make your test work, you need to reset the destination string instead of concatenating the string twice to it and hence observing it printed twice, so this should work as you expect

#include <string.h>
#include <stdio.h>

char *my_strcat(char *dest, const char *src)   {
    char *dst = dest;

    while (*dest!= '\0')
        dest++;

    while ((*dest++ = *src++) != '\0') {}

    return dst;
}

int main(int argc, char **argv) {
    // char string1[25] = "C";
    char string1[25];
    char string2[] = " programming " ;

    string1[0] = '\0';
    printf("strcat   : %s\n", strcat(string1, string2));
    string1[0] = '\0';
    printf("my_strcat: %s\n", my_strcat(string1, string2));
}
Iharob Al Asimi
  • 52,653
  • 6
  • 59
  • 97
1

You append "programming" once with strcat() and another time with my_strcat(). In total that's two appended "programming" strings, and they both get printed. It's expected behaviour.

sth
  • 222,467
  • 53
  • 283
  • 367