Advertisement
Guest User

Untitled

a guest
Feb 23rd, 2018
72
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 0.44 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <string.h>
  4.  
  5. int main(){
  6.     int len=7;
  7.     // No errors with this:
  8.     //char s1[len];
  9.     //char s2[len];
  10.  
  11.     // Valgrind error with this:
  12.     char *s1 = malloc(len * sizeof(char));
  13.     char *s2 = malloc(len * sizeof(char));
  14.  
  15.     strcpy(s1, "String1");
  16.     strcpy(s2, "String2");
  17.  
  18.     size_t new_size = strlen(s1) + strlen(s2) + 1;
  19.  
  20.     strncat(s1, s2, new_size);
  21.  
  22.     puts(s1);
  23.  
  24.     free(s1);
  25.     free(s2);
  26.     return 0;
  27. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement