20
int numbers[20];
int * p;    

Are the two assignments below the same?

p = numbers;
p = &numbers[0];
fredoverflow
  • 256,549
  • 94
  • 388
  • 662
user784637
  • 15,392
  • 32
  • 93
  • 156
  • 1
    Although it is not a duplicate, but it is interesting to note the related syntax : [Difference between &(*similarObject) and similarObject? Are they not same?](http://stackoverflow.com/questions/6958520/difference-between-similarobject-and-similarobject-are-they-not-same) – Nawaz Oct 08 '11 at 17:41
  • @Nawaz: What is the purpose of linking a completely different Q with this? How are they related? If anything needs to be linked as useful to the OP it has to be the FAQ entry: [How do I use arrays in C++](http://stackoverflow.com/questions/4810664/how-do-i-use-arrays-in-c).Please refrain from linking irrelevant Q's or Answers just because they were answered by you instead link relevant correct answers. – Alok Save Oct 08 '11 at 17:58
  • 2
    @Als: I find that interesting and somehow related, syntax-wise, hence I posted as a comment, not as duplicate. A link to an interesting topic is far better than many of your comments which you post, plus since it is just a comment, so cheer. No need to policing on every small thing. – Nawaz Oct 08 '11 at 18:05
  • 1
    @Nawaz: `somehow related` how? I don't see any relation. If each user starts linking irrelevant(that ofcourse maybe Interesting) links, there is no value addition just lot of noise.And refrain from personal attacks and if you do, cite references to your `far better than many of your comments which you post` quote.When experienced users like yourself bump their own answers it calls for every bit of policing. – Alok Save Oct 08 '11 at 18:05
  • @Als: Fine if you don't see any relation. And if users start posting useless comments which links to neither any interesting topic nor any funny jokes, then blah blah blah. I don't want to explain everything to you, neither am I obliged to. – Nawaz Oct 08 '11 at 18:07
  • 3
    @Als: Also note that finding something somehow related is a subjective thing; it all depends on how one thinks, visualizes concepts & notations and relates them. if you don't find it related then I'm fine with it. I'm not forcing anyone to click on the link and read the topic. – Nawaz Oct 08 '11 at 18:14
  • @Als: if you think so, then ask yourself why did I not post any other answer as link here? After all, I've posted more than 1500 answers, I could have given [this](http://stackoverflow.com/questions/6331588/sizeof-taking-two-arguments) link instead. But I did not. Why? – Nawaz Oct 08 '11 at 18:26

3 Answers3

25

Yes both are same.

In this case, Name of the array decays to a pointer to its first element.

Hence,

p = numbers;       //Name of the array

is same as:

p = &numbers[0];   //Address of the First Element of the Array
fredoverflow
  • 256,549
  • 94
  • 388
  • 662
Alok Save
  • 202,538
  • 53
  • 430
  • 533
16

Yes, they are the same. When an array's name is invoked in an rvalue context, it decays to a pointer to its first element.

David Heffernan
  • 601,492
  • 42
  • 1,072
  • 1,490
Puppy
  • 144,682
  • 38
  • 256
  • 465
2
numbers[0]    is equal to     *number    and equal to *(number+0)
numbers[x] = *(number+x)

so &(*(number+x) ) = number+x which is the address of x'th element

shotex
  • 349
  • 5
  • 10