Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <stdlib.h>
- #include <string.h>
- char *safecat(char *s, const char *ct)
- {
- char *p;
- unsigned int i;
- p = realloc(s, sizeof(s) + sizeof(ct) - 1); /* only allocate space for one '\0' */
- if (p == NULL){
- return NULL;
- }
- s = p; /* p now also points to first element */
- while(*p != '\0') p++;
- for(i = 0; i < strlen(ct); i++){
- *(p + i) = ct[i];
- }
- *(p + i) = '\0';
- return s;
- }
- int main()
- {
- char *str1 = "Twin";
- char *str2 = "Peaks";
- str1 = safecat(str1, str2);
- if (str1 == NULL){
- printf("Error: Could not connect strings\n");
- free(str1);
- return 1;
- }
- printf("%s\n", str1);
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement