I'm trying to write a function in which two words are appended into a third string, and this function must use malloc()
. I'm first writing it in the main before I put it in a function. I have:
int main(void){
char *word = "Johnny";
char *word2 = " Boy";
char *buff = malloc(100 * sizeof(char));
printf("%d\n", sizeof(buff));
int ct;
for(ct = 0; ct < strlen(word); ct++){
buff = word;
}
printf("%s\n", buff);
printf("the counter is now %d\n", ct);
buff += ct;
for(ct; ct < 13; ct++){
buff = word2;
}
printf("%s\n", buff);
}
I want buff to say "Johnny Boy" but at the end of it, "Johnny" is overwritten, and it just says " Boy"