Search code examples
c++pointersdereferencepointer-arithmeticaddressof

When I add up in my pointer address it points to my array? Why? Reference and Dereference confusion C++


In this example, I have an array of four elements. I have declared a pointer to integer which contains the address of array. Then i have displayed the address of 0th index in 3 different ways. Similarly, the address of 1st index is displayed in 3 different ways. When I output this (&pNumbers)+0 its pretty understandable that a unique and different address would be displayed. In next line, *(&pNumbers+0) displays the 0th index address (as its contain by the pointer). Now the problem part comes. On outputting this (&pNumbers)+1 line it displays the array 0th index again. Why ?

first question: when i retrieve the pointers address (&pNumbers), it displays new and unique address. But when i add up in that address. How is it accessing the array's element addresses? e.g

enter code here

cout<<" (&pNumbers)+1 "<<(&pNumbers)+1<<endl // new address + 1 but showing address of 0 element.

cout<<" (&pNumbers + 2) "<<(&pNumbers+2)<<endl // showing address of of 1st index

second question: If say it does somehow points to the corresponding array elements. On my dereferencing why it is not displaying the correct data value corresponding to array elements. e.g

enter code here
cout<<" *(&pNumbers+1) "<< *(&pNumbers+1) // assumption was 31 (but displaying some 0x1f)

cout<<" *(&pNumbers + 2) "<<*(&pNumbers+2)<<endl // assumption was 28 (but displpaying 0x1c)

Below is the 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 but 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;
}

Solution

  • this is memory layout of your stack on most platforms. your stack

    On outputting this (&pNumbers)+1 line it displays the array 0th index again. Why ?

    Your code gets address of pNumbers memory cell (type of &pNumbers is int **), then adds one to it which semantics is to increment it by size of pointer, so expression &pNumbers + 1 is an address of your original array as pNumbers variable is a pointer itself.

    To your second question what expression *(&pNumbers+1) is doing is basically trying to interpret memory location which stores int as pointer to int (type of *(&pNumbers+1) in int *). This is the reason why you see your int in hex instead to dec format. In other words you treat number[0] as int * and print it.

    What can be confusing in all this stuff is that array is treated as pointer to its first element which causes that number and &number is essentially the same address (but both expressions has different types)

    Hope could help.