I wonder why in the following code example 1 is written like it is and not like I wrote down in example 2. What are the differences between them and is there a reason for having the * outside of the ()?
Code:
#include <iostream>
const int ARRAY_LENGHT = 5;
int main()
{
using namespace std;
// an array of 5 integers initialized to 5 values
int Numbers [ARRAY_LENGHT] = {0, 100, 200, 300, 400};
// pInt points to the first element
const int *pInt = Numbers;
cout << "Using a pointer to print the contents of the array: " << endl;
for (int nIndex = 0; nIndex < ARRAY_LENGHT; ++ nIndex)
{
cout << "Element [" << nIndex << "] = " << *(pInt + nIndex) << endl;
}
return 0;
}
Example 1:
Code:
cout << "Element [" << nIndex << "] = " << *(pInt + nIndex) << endl;
Example 2:
Code:
cout << "Element [" << nIndex << "] = " << (*pInt + nIndex) << endl;