I just encountered an input statement for char array in c++ and I didn't understand how it works.
char s[100];
cin >> s + 1;
cout << s + 1;
Input : Harold
Output: Harold
Can anyone explain to me how it works?
When you give the input 'Harold', the indices from 1 to 6 (As you do with s+1) will be assigned with the given characters. Other indices still contain garbages.
This array looks like this: {'X','H','a','r','o','l','d','X','X',........} (Here 'X' means garbage)
But the thing is that when you want to get this array as output from s+1 (literally s[1]), It shows only the value you have assigned. Hence the output 'Harold'.
chararray. Sorry we don't know how much of C++ you know, so it's a bit difficult to pick a starting point for an explanation. Why exactly are you confused about these lines?((&s[0]) + 1)would you understand it?char s[100] = "a";Now at the end addcout << '\n' << s << '\n';With that, can you tell what is going on?