I am trying to implement my own str concat function in C++, however one of my pointer variables is not being assigned to another pointer value. When I debug my program in CLION, it takes me to the line *p = *s2
and it assigns my *p
variable to \0
instead of b
. I am not sure where this error is coming from and can I also get help on how to debug this further in cLion?
I've tried changing the second char pointer in the parameter to const, and googling the answer for EXC_BAD_ACCESS (code=2, address=0x10c226edd)
.
char * my_strcat(char* s1, char* s2)
{
if(s1 == NULL || s2 == NULL) {
return NULL;
}
char *p = s1;
while (*p != '\0')
{
p++;
}
while (*s2 != '\0')
{
*p = *s2;
p++;
s2++;
}
*p = '\0';
return s1;
}
char *arr4 = "Taco";
char *arr5 = "bean";
std::cout << my_strcat(arr4,arr5) << std::endl;
Expected results are arr4
and arr5
to be concatenated, nothing is printed as the actual result.