First I have a function to initialize an array and return a pointer pointed to its first element.
int* getPtrToArray()
{
int array[10] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
return array;
}
And this function just creates an array not going to be used.
void testing()
{
int junk[3] = {1234, 5678, 9101112};
}
Here is my main function:
int main()
{
// #1
int array[10] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
int* ptr0 = array;
cout << *ptr0 << endl;
cout << ptr0[0] << endl;
// #2
int* ptr1 = getPtrToArray();
cout << *ptr1 << endl;
cout << ptr1[0] << endl;
// #3
testing();
cout << *ptr1 << endl;
cout << ptr1[0] << endl
}
The output results are:
1
1
1
2066418736 // "ptr1[0]" should be the same as "*ptr1", right?
2066418736 // since testing() does not modify the array, why "*ptr1" is changed?
2066418736
I think all these six outputs should be 1
(the first element in the array). Could anyone explains this to me? Thanks!