0

I understand something like this:

    int foo = 5;
    int * fooPointer = &foo;

but I know you can also do something like this:

    int foo[5] = {32,12,4};
    int * fooPtr = food;

Which is strange because when you do it with the int you have to put the & operator before the foo, but you don't have to if it is an array. You can declare a pointer and then say it is an array?

Remy Lebeau
  • 555,201
  • 31
  • 458
  • 770
Gabriel
  • 11
  • 4
  • 2
    see: http://stackoverflow.com/questions/1461432/what-is-array-decaying – Red Alert Mar 16 '15 at 23:54
  • Your `int *` pointer cannot point to the entire array. It can only point to a specific element of the array. And you *do* have to use `&` for that: `fooPtr = &foo[0]` or `fooPtr = &foo[3]`. However, the `fooPtr = &foo[0]` version can be replaced with a "shorthand" form `fooPtr = foo`, which is made possible by *array type decay*. In other words, the variant without `&` is a niche special case. Don't get mislead by that niche special case. In general case you will need to use `&` with arrays as well. – AnT stands with Russia Mar 17 '15 at 00:01

1 Answers1

0

It's not a list; it's an array.

Under most circumstances, the name of an array evaluates to the address of the first element of the array, which you can assign to (or use to initialize) a pointer to the element type of the array.

There are a few exceptions to that (passing the array to sizeof or the address-of operator (unary &), or passing the array by reference, but none of those applies here.

Jerry Coffin
  • 476,176
  • 80
  • 629
  • 1,111