Q:

Confusion About Array and String in C

What is the difference between S1, S2 and S3? 
char S1[6];
S1[0] = 'A';
S1[1] = 'r';
S1[2] = 'r';
S1[3] = 'a';
S1[4] = 'y';

char S2[6] = {'A','r','r','a','y'};

string S3 = "Array";

When I run the program using if (strcmp(a,b) == 0), where a, b = S1, S2, S3.
It shows that S2 and S3 are the same, and S1 and S2 is different. 
Why is this the case?? 
Why not all three are equivalent? 
And when I add back '\0' to both S1b, S1c. All 3 are the same. This is understandable. 
BUT why in my first trial, S2 and S3 are the same then?? I did not include '\0' too. And I suspect S1 and S2 should be the same, but not S2 and S3.
Can anyone tell me why my thought is wrong???
Thanks for your answers. I have tried and changed the settings to the followings:
char S1[5];
S1[0] = 'A';
S1[1] = 'r';
S1[2] = 'r';
S1[3] = 'a';
S1[4] = 'y';

char S2[5] = {'A','r','r','a','y'};

string S3 = "Array";

And now clearly S2 and S3 are not the same, since they differs by a '\0'. 
However, I am still a bit confused why S1 and S2 are not the same this time again if I use strcmp to compare the two?

A:

Compare the actual in-memory values of the arrays:

S1 is 6 elements big, yet you only specify values for 0-5, the 6th element is not explicitly set, so it retains whatever value the memory location had prior to allocation.
S2 is similar to S1 , only 5 elements are provided, however when using the {,} syntax any extra elements are zeroed. So char foo[5] = { 1, 2 } is identical to char foo[5] = { 1, 2, 0, 0, 0}.
S3 uses the string syntax way of initialising an array, which creates an array of char (or wchar_t) with an extra element set to \0 (the null terminator).

Visually:
S1 = 0x41, 0x72, 0x72, 0x61, 0x79, 0x??
S2 = 0x41, 0x72, 0x72, 0x61, 0x79, 0x00
S3 = 0x41, 0x72, 0x72, 0x61, 0x79, 0x00

Note that you're running into a safety problem with strcmp: it doesn't have a length parameter, it keeps on searching until it encounters \0, which might be never (i.e. until it causes a segfault or access violation). Instead use a safer function like strncmp or (if using C++) the std::string type.

