user3514491
user3514491

Reputation: 75

When passing the address of the first element in an array, why write &Array[0] instead of Array?

In many code examples I find, they use "&Array[0]" if an array has to be passed to a function. Is there any reason to do so ? They could simply write "Array".

Upvotes: 1

Views: 84

Answers (1)

Adrian Ratnapala
Adrian Ratnapala

Reputation: 5683

This is a pure style issue, and you should just do whatever your dev team prefers. Personally I like to use plain old Array and if I want to offset by n elements I would do Array + n. But some people find that confusing and use &Array[n], even when n == 0.

In defense of the other side the Array + n syntax requires you to know more about C in order to understand it. It involves both arrays degenerating into pointers, and also pointer arithmetic. But in my view, C programmers need to know all that stuff anyway, it's not a complicated language.

Upvotes: 4

Related Questions