Take the 2-minute tour ×

I know that int (*p)[5] means a pointer which points to an array of 5 ints. So I code this program below:

#include <iostream>
using namespace std;

int main()
  int a[5]={0,1,2,3,4};
  int (*q)[5]=&a;
  return 0;

On my machine the result is:

0xbfad3608       //?__?

I can understand that *q means the address of a[0] and **q means the value of a[0], but why does q have the same value as a and *q? In my poor mind, it should be the address of them! I'm totally confused. Somebody please help me. Please!

share|improve this question

3 Answers 3

up vote 8 down vote accepted

Look at it this way:

   q == &a
   *q == a
   **q == *a

You didn't try printing &a. If you do, you'll see that it has the same value as a. Since &a == a, and q == &a, and *q == a, by transitivity q == *q.

If you want to know why &a == a, check out Why is address of an array variable the same as itself?

share|improve this answer
Does it means that &a==a==&a[0]? –  zyc Nov 30 '11 at 8:19
@zyc, no, but they all start at the same address. –  avakar Nov 30 '11 at 8:19
Yes, &a == a == &a[0]. –  rob mayoff Nov 30 '11 at 8:20
@zyc: yes and no, this is known as decay, ie an array in most contexts will decay into a pointer, and the value of such pointer is just the address of its first element, which also happens to be the address of the array itself. –  Matthieu M. Nov 30 '11 at 8:21
Oh,I get it! thank you! –  zyc Nov 30 '11 at 8:23

q and &a are pointers to the array.

*q and a are "the array". But you can't really pass an array to a function (and std::ostream::operator<< is a function); you really pass a pointer to the first element, which is created implicitly (called pointer decay). So *q and a become pointers to the first element of the array.

The beginning of the array is at the same location in memory that the array is, trivially. Since none of the pointers involved are pointers-to-char (which are handled specially so that string literals will work as expected), the addresses just get printed out.

share|improve this answer

That because array is automatically converted to a pointer, which just has the value of the address of the array. So when you are trying to print print the array using <<a or <<*q, you are in fact printing its address.

share|improve this answer
Yes,but I just want to print the address of it. –  zyc Nov 30 '11 at 8:30

Your Answer


