user784637
user784637

Reputation: 16122

Is p = array the same as p = &array[0]?

int numbers[20];
int * p;    

Are the two assignments below the same?

p = numbers;
p = &numbers[0];

Upvotes: 20

Views: 7315

Answers (3)

shotex
shotex

Reputation: 381

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

Upvotes: 2

Alok Save
Alok Save

Reputation: 206528

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

Upvotes: 25

Puppy
Puppy

Reputation: 146910

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.

Upvotes: 16

Related Questions