guys,
I made this program and I would like to have your opinion and suggestions if this is correct; I having trouble with malloc (), the objective here is I need to allocate enough memory for the variable newstring after it was concatenated using strcat function and I have no idea how to check if my malloc() was allocated the space for the variable newstring.
Appreciate your positive response.Code:/* This will use malloc() function to allocate */ /* memory for the concatenated two strings */ #include <stdio.h> #include <string.h> #include <stdlib.h> char *conca_str(char *, char *); int main( void ) { char *str1 = "Hello"; char *str2 = " World!"; char *join; join = conca_str(str1, str2); printf("%s\n", join); free(join); return 0; } char *conca_str(char *string1, char *string2) { char *conca, *newstring; newstring = strcat(string1, string2); conca = (char *) malloc (100); conca = newstring; return conca; }
Regards,
pvrgallardo