When I add up in my pointer address it points to my array? Why? Reference and Dereference confusion C++ -
in example, have array of 4 elements. have declared pointer integer contains address of array. have displayed address of 0th index in 3 different ways. similarly, address of 1st index displayed in 3 different ways. when output (&pnumbers)+0 pretty understandable unique , different address displayed. in next line, *(&pnumbers+0) displays 0th index address (as contain pointer). problem part comes. on outputting (&pnumbers)+1 line displays array 0th index again. why ?
first question: when retrieve pointers address (&pnumbers), displays new , unique address. when add in address. how accessing array's element addresses? e.g
enter code here cout<<" (&pnumbers)+1 "<<(&pnumbers)+1<<endl // new address + 1 showing address of 0 element. cout<<" (&pnumbers + 2) "<<(&pnumbers+2)<<endl // showing address of of 1st index
second question: if somehow points corresponding array elements. on dereferencing why not displaying correct data value corresponding array elements. e.g
enter code here cout<<" *(&pnumbers+1) "<< *(&pnumbers+1) // assumption 31 (but displaying 0x1f) cout<<" *(&pnumbers + 2) "<<*(&pnumbers+2)<<endl // assumption 28 (but displpaying 0x1c)
below source code:
#include <iostream> #include <string> #include <conio.h> using namespace std; int main() {int number[] = { 31, 28, 31, 30}; int *pnumbers = number; cout<<" address of number[0] "<<number<<endl; cout<<" address of number[1] "<<number+1<<endl; cout<<" address of number[2] "<<number+2<<endl; cout<<" address of number[3] "<<number+3<<endl<<endl; cout<<" address of &pnumbers "<< &pnumbers<<endl<<endl; // address of pnumbers cout<<" address of number "<< number<<endl; // address of array's first element cout<<" pnumber address "<< pnumbers<<endl; cout<<" &(pnumbers[0]) "<< &(pnumbers[0])<<endl<<endl; cout << " pnumbers+1: " << pnumbers+1<<endl; //address of array's second element cout<<" (&pnumbers[1]) "<<(&pnumbers[1])<<endl; // cout<<" (pnumbers+1) "<< (pnumbers+1) <<endl<<endl; cout<<" (&pnumbers)+0 "<< (&pnumbers)+0<<endl; cout<<" *(&pnumbers+0) "<< *(&pnumbers+0)<<endl<<endl; cout<<" (&pnumbers)+1 "<<(&pnumbers)+1<<endl<<endl; // new address + 1 expected displaying array's 0th index address why ? cout<<" *(&pnumbers+1) "<<*(&pnumbers+1)<<endl<<endl; cout<<" (&pnumbers + 2) "<<(&pnumbers+2)<<endl<<endl; cout<<" *(&pnumbers + 2) "<<*(&pnumbers+2)<<endl<<endl; cout<<" (&pnumbers + 3) "<<(&pnumbers+3)<<endl<<endl; cout<<" *(&pnumbers + 3) "<<*(&pnumbers+3)<<endl<<endl; cout<<" (&pnumbers + 4) "<<(&pnumbers+4)<<endl<<endl; cout<<" *(&pnumbers + 4) "<<*(&pnumbers+4)<<endl<<endl; return 0; }
this memory layout of stack on platforms.
on outputting (&pnumbers)+1 line displays array 0th index again. why ?
your code gets address of pnumbers
memory cell (type of &pnumbers
int **
), adds 1 semantics increment size of pointer, expression &pnumbers + 1
address of original array pnumbers
variable pointer itself.
to second question expression *(&pnumbers+1)
doing trying interpret memory location stores int
pointer int (type of *(&pnumbers+1)
in int *
). reason why see int in hex instead dec format. in other words treat number[0]
int *
, print it.
what can confusing in stuff array treated pointer first element causes number
, &number
same address (but both expressions has different types)
hope help.
Comments
Post a Comment