char *B ="";
This means that B
is a pointer and you have pointed to the constant string ""
.
Copy the A
string to the B
with strcpy(B,A);
This means that you are copying the string of A
to the memory which the B
is pointing (and B
is pointing to a constant string) so this will cause undefined behaviour
To avoid a such problem the B pointer should be pointed to a memory space for example you can point the B to a dynamic memory space allocated with malloc:
char *B = malloc(10*sizeof(char));
and you can make the size of the memory exactely the same of A string size with:
char *B = malloc((strlen(A)+1)*sizeof(char));
The +1 is for the null terminator character for the string
Another solution to avoid the problem: Define the B as an array of chars and not as a pointer:
char B[10];