I want to copy one char *
string into another char *
variable so I have the same string stored twice into those two char *
variables but that they do not depend on each other, so if I make a free
of the first one, I don't lose the data on the second one.
My question is, can I just execute the strcpy without allocating the second char *
string? Is any of the following options correct / incorrect?
Option 1
//char *string_1 is already allocated and contains a valid string
char *string_2;
strcpy(string_1, string_2);
printf("%s", string_2);
Option 2
//char *string_1 is already allocated and contains a valid string
char *string_2;
string_2 = malloc(((int) strlen(string_1)) * sizeof(char));
strcpy(string_1, string_2);
printf("%s", string_2);