0

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;
}
user1155921
  • 62
  • 1
  • 7
  • `assumption was 31 (but displaying some 0x1f)` 0x1f in hexdecimal **is** 31 in decimal. – PaulMcKenzie May 31 '15 at 08:05
  • I would assume it's because pNumbers is on the stack right before numbers[], this will be compiler dependent, and indeed when I run it I see different output. – Richard Viney May 31 '15 at 08:10
  • And for your second question, the number 28 in base ten *is* the same number as 0x1C in hexadecimal. They are equal. – Richard Viney May 31 '15 at 08:11

1 Answers1

0

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.

robal
  • 368
  • 2
  • 8
  • Thanks for replying :) I pretty much understood the reason of second question. But am still a bit stuck on question 1. – user1155921 May 31 '15 at 09:07
  • Above the line `int *pNumbers = number;` add another line `int x = 0;` and look what `(&pNumbers)+1` is. It will be the same as `&x` (on your particaular archtecture). So `&pNumbers+1` points to whatever is on your stack declared before `pNumbers`. – robal May 31 '15 at 09:43
  • Yikes, I am afraid its not happening as you have stated in your explanation. number[0] address 0x28fee0, number[1] address 0x28fee4, number[2] address 0x28fee8, number[3] address 0x28feec, &pNumbers address 0x28fedc, &x address 0x28fed8, (&pNumbers)+1 address 0x28fee0, – user1155921 May 31 '15 at 20:03
  • It looks like you put declaration of `x` below declaration of `pNumbers` as `pNumbers` variable has greater address than `x`. And make sure to disable optimizations in your compiler, with gcc you can use -O0, maybe for some reason your compiler rearange your declarations. – robal May 31 '15 at 20:43