So I'm experimenting with pointers and arrays trying to get the feel of using and understanding them more. The *ptArray value in my source seems to change and not stay equal to 0 After the 2nd for loop? I'm puzzled as to why this occurs I thought *ptArray would retain its value of 0.
Try using the address-of operator (&), but you cannot point to the whole array. I would use a for loop and take the address of one array value at a time like this:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
//declare variables
int array[5] = {0, 5, 6, 3, 4};
int *pointer = nullptr; //set to NULL or nullptr
//loop through array
for (int i = 0; i < 5; i++)
{
//store address of array value in pointer
pointer = &array[i];
//display values
cout << "array[" << i << "] value: " << array[i] << endl;
cout << "pointer value: " << pointer << endl;
cout << "*pointer value: " << *pointer << endl << endl;
} //end for