[Solved] Difference between assignment by = sign and strcopy [duplicate]


Actually, if you print the value of x before and after calling:

x = "test";

You will see that it has changed. By losing a track to your allocated memory, you face with memory leak here.

Furthermore, printf prints a string that starts from the pointer position until it finds the string terminated ‘\0’ (0).

Suggested solution:

char* x = malloc(5);    /* sizeof(char) is always equal to 1 */
strcpy(x, "test");

2

solved Difference between assignment by = sign and strcopy [duplicate]