|
Simple C++ question
Learning c++ on my own - why would printing *yPtr output 0?
1 #include <iostream >
2 const int ARRAY_LEN = 10;
3
4 int main() {
5 int arr[ARRAY_LEN] = {10}; // Note implicit initialization of
6 // other elements
7 int *xPtr = arr, *yPtr = arr + ARRAY_LEN -1;
8 std::cout << *xPtr << ’ ’ << *yPtr; // Should output 10 0
9 return 0;
10 }
from my understanding, line 5 initiates an array of 10 elements, all of which are equal to 10.
Then line 7, *yPtr is a pointer that points to the first element of "arr", plus 10 - 1 ie 9 elements past the first element of "arr", ie the 10th element of the array which is equal to 10. Yet cout << *yPtr outputs 0, not 10. What it do?
Thanks in advance.
|