#include<stdio.h>
#include<string.h>
#include<stdlib.h>
void catTo(char *dest, const char *str1, const char *str2)
{
dest = (char *) malloc(1 + strlen(str1)+ strlen(str2) );
strcpy(dest, str1);
strcat(dest, str2);
}
int main(int argc, char** argv) {
char *str1 = "abcd";
char *str2 = "defg";
char *dest;
catTo(dest, str1, str2);
printf("%s",dest);
return 0;
}
I have been trying to get a simple str copy function to work, however, when I print out dest, I get "(null)". I tried, messing around with putting &/* infront of certain variables, but to no prevail.
void catTo(char *dest,:destwon't be modified outside your function.gcc, at a minimum use:-Wall -Wextra -pedanticI also use:-Wconversion -std=gnu99)void*so can be assigned to any other pointer. Casting just clutters the code, making it more difficult to understand, debug, maintain. 2) always check (!=NULL) the returned value to assure the operation was successful