When I run the code below, my output is not what I expect.
My way of understanding what ptrpoints to the address of the first element of the array Str. I think that ptr + 5should lead to the + 5th element, which is equal f. Thus, the output should only be displayed f, not both fg.
Why is he showing fg? Is this due to how coutthe array is displayed?
#include <iostream>
using namespace std;
int main()
{
char *ptr;
char Str[] = "abcdefg";
ptr = Str;
ptr += 5;
cout << ptr;
return 0;
}
Expected Result: f
Actual output: fg
source
share