+ 1

Pointer

I still get confused on this. Can i put the different number on this "int apple[x]", and how do we know what the output for this? Here is my code : #include <iostream> using namespace std; int main() { int Apple[5]; int*bp0 = &Apple[0]; int*bp1 = &Apple[1]; int*bp2 = &Apple[2]; int*bp3 = &Apple[3]; int*bp4 = &Apple[4]; cout << "Apple is at:" << bp0 << endl; cout << "Apple is at:" << bp1 << endl; cout << "Apple is at:" << bp2 << endl; cout << "Apple is at:" << bp3 << endl; cout << "Apple is at:" << bp4 << endl; system("PAUSE"); return 0; }

16th May 2018, 1:21 PM
Lyn
Lyn - avatar
2 Answers
+ 1
first you didnt initialized the array so it contain garbage value and output is unpredicted, second in cout you are using name of pointer variable so it print address of array at which location it is pointing, if you want to print value stored in array use "*" before variable name. like cout<<*bp0;
16th May 2018, 1:43 PM
Tarun Sharma
Tarun Sharma - avatar
0
yes, Lyn you may put different number on 'x' x is defined as the size of the array. it will store the values as addresses inside the pointer constants prints it on the screen
16th May 2018, 1:43 PM
Rajeeb