20
int numbers[20];
int * p;    

Are the two assignments below the same?

p = numbers;
p = &numbers[0];
7
  • 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? Commented Oct 8, 2011 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++.Please refrain from linking irrelevant Q's or Answers just because they were answered by you instead link relevant correct answers. Commented Oct 8, 2011 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. Commented Oct 8, 2011 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. Commented Oct 8, 2011 at 18:05
  • 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. Commented Oct 8, 2011 at 18:14

3 Answers 3

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
Sign up to request clarification or add additional context in comments.

Comments

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.

Comments

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.