You have multiple failures; the mysterious thing is that you do not get a general protection error or a SIGSEGV.
struct mystruct *entrada;
There you are creating a pointer to your structure, which initially has an undetermined value and we do not know where it points. Your code does not show how you initialize that pointer.
strcpy ( entrada[i].arr, g);
When i == 0
, you are copying the content of a string on the first element of a supposed array of your structures , not on the first element of the array arr
of your structure.
When i == 1
, you are copying the contents of a string over the second element of an assumed array of your structures , not over the second element of the array arr
of your structure.
strcpy( destino, origen );
This function goes through the memory, from the position marked by origen
, and is copying bytes to destino
, sequentially until it finds a byte \ 0, that also is copied .
That means that when you do
char g[20]={ "11111111111111111118" };
for (int i = 0; i < 2; ++i) {
strcpy ( entrada[i].arr, g);
}
Actually, you are copying 2 times the entire string g [20] , starting the second copy just after the data copied the first time strong>.
If your entrada
is effectively an array, has happened that, due to its size and alignment, its memory spaces are just one behind the other . That means that the second call to strcpy()
overwrites the byte \ 0 that the first call put at the end of entrada[0]
. That overwrite causes printf()
to see no separation between the strings, and show them together.
Although in C
the pointers and arrays are equivalent, you are mixing them in a way, at least, curious .