Advertisement
Guest User

Untitled

a guest
Jan 17th, 2017
75
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 0.65 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <string.h>
  4.  
  5. char *safecat(char *s, const char *ct)
  6. {
  7.     char *p;
  8.     unsigned int i;
  9.     p = realloc(s, sizeof(s) + sizeof(ct) - 1); /* only allocate space for one '\0' */
  10.     if (p == NULL){
  11.         return NULL;
  12.     }
  13.     s = p; /* p now also points to first element */
  14.     while(*p != '\0') p++;
  15.     for(i = 0; i < strlen(ct); i++){
  16.         *(p + i) = ct[i];
  17.     }
  18.     *(p + i) = '\0';
  19.     return s;
  20. }
  21.  
  22.  
  23. int main()
  24. {
  25.     char *str1 = "Twin";
  26.     char *str2 = "Peaks";
  27.     str1 = safecat(str1, str2);
  28.     if (str1 == NULL){
  29.         printf("Error: Could not connect strings\n");
  30.         free(str1);
  31.         return 1;
  32.     }
  33.     printf("%s\n", str1);
  34.     return 0;
  35. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement