Open main menu
Home
Random
Recent changes
Special pages
Community portal
Preferences
About Wikipedia
Disclaimers
Incubator escapee wiki
Search
User menu
Talk
Dark mode
Contributions
Create account
Log in
Editing
C (programming language)
(section)
Warning:
You are not logged in. Your IP address will be publicly visible if you make any edits. If you
log in
or
create an account
, your edits will be attributed to your username, along with other benefits.
Anti-spam check. Do
not
fill this in!
=== Array–pointer interchangeability === The subscript notation <code>x[i]</code> (where <code>x</code> designates a pointer) is [[syntactic sugar]] for <code>*(x+i)</code>.<ref name="Raymond1996">{{cite book |last1=Raymond |first1=Eric S. |author-link=Eric S. Raymond |title=The New Hacker's Dictionary |edition=3rd |url={{GBurl|id=g80P_4v4QbIC|p=432}} |access-date=August 5, 2012 |date=October 11, 1996 |publisher=MIT Press |isbn=978-0-262-68092-9 |page=432 }}</ref> Taking advantage of the compiler's knowledge of the pointer type, the address that <code>x + i</code> points to is not the base address (pointed to by <code>x</code>) incremented by <code>i</code> bytes, but rather is defined to be the base address incremented by <code>i</code> multiplied by the size of an element that <code>x</code> points to. Thus, <code>x[i]</code> designates the <code>i+1</code>th element of the array. Furthermore, in most expression contexts (a notable exception is as operand of <code>[[sizeof]]</code>), an expression of array type is automatically converted to a pointer to the array's first element. This implies that an array is never copied as a whole when named as an argument to a function, but rather only the address of its first element is passed. Therefore, although function calls in C use [[pass-by-value]] semantics, arrays are in effect passed by [[reference (computer science)|reference]]. The total size of an array <code>x</code> can be determined by applying <code>sizeof</code> to an expression of array type. The size of an element can be determined by applying the operator <code>sizeof</code> to any dereferenced element of an array <code>A</code>, as in <code>n = sizeof A[0]</code>. Thus, the number of elements in a declared array <code>A</code> can be determined as <code>sizeof A / sizeof A[0]</code>. Note, that if only a pointer to the first element is available as it is often the case in C code because of the automatic conversion described above, the information about the full type of the array and its length are lost.
Edit summary
(Briefly describe your changes)
By publishing changes, you agree to the
Terms of Use
, and you irrevocably agree to release your contribution under the
CC BY-SA 4.0 License
and the
GFDL
. You agree that a hyperlink or URL is sufficient attribution under the Creative Commons license.
Cancel
Editing help
(opens in new window)