0

I thought array and pointer are basically the same thing, until I run this program:

int main() {
  int* a = new int(19);
  int b[1];
  b[0] = 19;

  printf("*a: %d\n a: %p\n &a:%p\n", *a, a, &a);
  printf("*b: %d\n b: %p\n &b:%p\n", *b, b, &b);
  delete a;
}

output is:

*a: 19
 a: 0x7f94524000e0
 &a:0x7fff51b71ab8
*b: 19
 b: 0x7fff51b71ab4
 &b:0x7fff51b71ab4

can someone please explain why the output of &b is the same as b?

Thanks! -Erben

Erben Mo
  • 3,528
  • 3
  • 19
  • 32
  • This kind of arrays refers to a dead-memory in your program, and is different from pointers. Pointers can refer to any adress. Another thing: if you use `int tab[n][m]`, you can't use it with a `int **ptr` but with a `int *ptr` -> compiler will replace calls to `tab[i][j]` by `tab[i * m + j]`. – Pierre Emmanuel Lallemant Nov 02 '13 at 19:56

3 Answers3

6

Well, b is an array. Under the slightest excuse it will decay into a pointer to the first element of b. Note, however, that the expression b and &b are not equivalent: b decays into a pointer the first element, i.e., it is of type int* while &b is a pointer to the array, i.e., it is of type int(*)[1].

Dietmar Kühl
  • 150,225
  • 13
  • 225
  • 380
  • But why are a and &a different? – Silent Control Nov 02 '13 at 20:09
  • 2
    @Robertfrost: `a` and `&a` are clearly to different entities: `a` is an object which happens to be a pointer to an `int` and `&a` is that object's address, i.e., it is a pointer to a pointer to `int`. Arrays are special, though: if you just name and evaluate them, they automatically turn into a pointer to their first element, i.e., `b` is equivalent to `&b[0]`. This element lives at the same address as the start of the array object which is obtained from `&b`. – Dietmar Kühl Nov 02 '13 at 20:17
  • @Robertfrost: See [this answer](http://stackoverflow.com/questions/18518255/why-is-array-name-a-pointer-to-the-first-element-of-the-array/18519524#18519524) for some historical context. – John Bode Nov 02 '13 at 21:06
2

Arrays and pointers are not the same. A pointer can behave like an array (e.g. accessing by index).

&b is a pointer to the whole array and b is a pointer to the first element. They may point to a same address in memory but they are totally different things.

        +-------------------------------+  
        |+-----+-----+-----+-----+-----+|  
        ||     |     |     |     |     ||  
 &b---->||  0  |  1  |  2  | ... |  N  ||  
        ||     |     |     |     |     ||  
        |+-----+-----+-----+-----+-----+|  
        +---^---------------------------+  
            |                              
            b                              
masoud
  • 55,379
  • 16
  • 141
  • 208
  • a and &a are two different values because one is the address of that "0", one is the address of that "a". but in b, you only have one box and therefore one memory address. – Erben Mo Feb 10 '14 at 00:09
-1

a is a variable. You are allocating memory using new and assigning the result to this pointer. You might decide to store something else in a later in your program.

b is different. It's not a variable in the sense that it cannot store different addresses. It's an array, having a fixed start location.

Thus, b and &b are same. But the contents of a and the actual address of a are different.

Jaywalker
  • 3,079
  • 3
  • 28
  • 44