When defining an array, if you don't give the size then the size is determined from the size of the initializer. E.g.,
1 2 3 4 5 6 7 8
|
int a[] = {1, 2, 3, 4}; // This array is 4 elements long.
// The contents are 1, 2, 3, 4
int b[4] = {1, 2}; // This array is also 4 elements long.
// The contents would be 1, 2, 0, 0.
int c[4]; // This array is also 4 elements long.
// The contents are indeterminate.
|
You can use a string literal to initialize a char array. A string literal is one char longer than the chars between the quotes since a null char '\0' is added to the end. So "hello" is 6 chars long.
So with your arrays, s1 is 9 chars long and s2 is 5 chars long. You are trying to copy the contents of s1 to s2, but s2 is not long enough to hold the chars. Therefore the chars get written past the end of s2. In your screenshot we see that on your system the contents of s1 are stored directly after s2, so the chars that overflow s2 are written into the beggining of s1.
IcterusGalbula's result is different from yours because he is on a different system (linux vs windows, for example, or even just a different compiler on the same system). You could both print the addresses of your variables to see what order they are stored in:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
|
#include <iostream>
#include <cstring>
using namespace std;
int main()
{
char s1[] = "abcdefgh";
char s2[] = "ijkl";
cout << "s1: " << &s1 << "\ns2: " << &s2 << '\n';
cout << s1 << '\n' << s2 << '\n';
strcpy(s2, s1);
cout << s1 << '\n' << s2 << '\n';
}
|
My output shows that s1 is after s2 in memory for me:
s1: 0x7ffcdf6fa8e3
s2: 0x7ffcdf6fa8de
abcdefgh
ijkl
fgh
abcdefgh |
The upshot of this is to ensure that the char array you are copying to has enough room to hold the chars. To fix your code you could explicitly size the arrays so that s2 is at least long enough to hold the contents of s1.
1 2
|
char s1[50] = "abcdefgh";
char s2[50] = "ijkl";
|