Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <stdlib.h>
- #include <string.h>
- int main (int argc, char **argv){
- char *str;
- char str1[]="First";
- char str2[]=" Second";
- str = malloc(strlen(str1)+1);
- if(!str)
- {
- printf("error: cannot allocate memory block.\n");
- return 1;
- }
- strcpy(str, str1);
- printf("%s\n", str);
- //should be large enough to contain the concatenated resulting string
- char *strTmp = realloc(str,strlen(str1)+strlen(str2)+1);
- if(!strTmp)
- {
- printf("error: cannot reallocate memory block.\n");
- free(str);
- return 1;
- }
- str = strTmp;
- strcat(str, str2);
- printf("%s\n", str);
- free(str);
- return 0;
- }
- /*
- First
- First Second
- */
Advertisement
Add Comment
Please, Sign In to add comment