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".
1 Answer
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.
2 Comments
Ed Swangren
"But in my view, C programmers need to know all that stuff anyway" - Agreed. If you're working on a real project and don't know the semantics of arrays then you're already in a really bad spot. Production code should not be catered to beginners.
fkl
Having programmed in C for almost a decade, (7+ years in industry) I would also argue that if i see it written that way (&arr[0]), the first thing that comes to mind is, whether it is written by some one new to C? May be they have only read the chapter on arrays and not pointers yet? I really don't recall ever seeing any good open source / professional c code which indexes the first element that way. Would love to see an example.
&"the address of"Array"in an array"[0]"the first element" - you answer your own question :)