This is a hw assignment that I am having a lot of difficulty with. I'm tasked with creating my own function that accepts two strings and will concatenate the two strings together and will return a character pointer to the new string. This is what I currently have:
char * my_strcat(char p[], char t[]) {
printf("Made it into my_strcat.\n");
int size1;
int size2;
size1 = my_strlen(p);
size2 = my_strlen(t);
int size3;
size3 = size1 + size2 +1;
printf("This many characters allocated for in memory: %i\n", size3);
char* MemLoc = (char *)malloc(sizeof(char)*(size1+size2+1));
char* BookMark = MemLoc;
printf("Address of MemLoc: %p\n", MemLoc);
printf("Address of BookMark: %p\n", BookMark);
int count = 0;
while(count < size1) {
*BookMark = p[count];
printf("Address of MemLoc: %p\n", MemLoc);
printf("Latest char: %c\n", *MemLoc);
count++;
BookMark++;
printf("Address of MemLoc: %p\n", MemLoc);
}
count = 0;
while(count < size2) {
*BookMark = t[count];
printf("Latest char: %c\n", *MemLoc);
count++;
BookMark++;
}
*BookMark = '\0';
printf("Concatenated string: %s\n", MemLoc);
return MemLoc;
}
I have a lot of print statements in there trying to determine my error but I still can't pin it down. Depending on what type I print as, I am getting "nil", "null", and nothing for the last print statement. The last print statement (in my opinion) should be printing the final concatenated string.
Also, I had to construct the my_strlen method to calculate string length. This is working correctly and returns the correct length values of the strings sent in. Any suggestions for fixing this method?