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?
stringis not a standard C typeS1[5]is uninitialized; it might happen to contain'\0'.S2[5] == '\0', because when an object is initialized (distinct from assignment), any elements not specified are set to zero.stringas a typedef?