1 | <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
|
---|
2 | <html>
|
---|
3 | <!-- Copyright (C) 2022 Richard Stallman and Free Software Foundation, Inc.
|
---|
4 |
|
---|
5 | (The work of Trevis Rothwell and Nelson Beebe has been assigned or
|
---|
6 | licensed to the FSF.)
|
---|
7 |
|
---|
8 | Permission is granted to copy, distribute and/or modify this document
|
---|
9 | under the terms of the GNU Free Documentation License, Version 1.3 or
|
---|
10 | any later version published by the Free Software Foundation; with the
|
---|
11 | Invariant Sections being "GNU General Public License," with the
|
---|
12 | Front-Cover Texts being "A GNU Manual," and with the Back-Cover
|
---|
13 | Texts as in (a) below. A copy of the license is included in the
|
---|
14 | section entitled "GNU Free Documentation License."
|
---|
15 |
|
---|
16 | (a) The FSF's Back-Cover Text is: "You have the freedom to copy and
|
---|
17 | modify this GNU manual. Buying copies from the FSF supports it in
|
---|
18 | developing GNU and promoting software freedom." -->
|
---|
19 | <!-- Created by GNU Texinfo 6.7, http://www.gnu.org/software/texinfo/ -->
|
---|
20 | <head>
|
---|
21 | <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
|
---|
22 | <title>Accessing Array Elements (GNU C Language Manual)</title>
|
---|
23 |
|
---|
24 | <meta name="description" content="Accessing Array Elements (GNU C Language Manual)">
|
---|
25 | <meta name="keywords" content="Accessing Array Elements (GNU C Language Manual)">
|
---|
26 | <meta name="resource-type" content="document">
|
---|
27 | <meta name="distribution" content="global">
|
---|
28 | <meta name="Generator" content="makeinfo">
|
---|
29 | <link href="index.html" rel="start" title="Top">
|
---|
30 | <link href="Symbol-Index.html" rel="index" title="Symbol Index">
|
---|
31 | <link href="index.html#SEC_Contents" rel="contents" title="Table of Contents">
|
---|
32 | <link href="Arrays.html" rel="up" title="Arrays">
|
---|
33 | <link href="Declaring-an-Array.html" rel="next" title="Declaring an Array">
|
---|
34 | <link href="Arrays.html" rel="prev" title="Arrays">
|
---|
35 | <style type="text/css">
|
---|
36 | <!--
|
---|
37 | a.summary-letter {text-decoration: none}
|
---|
38 | blockquote.indentedblock {margin-right: 0em}
|
---|
39 | div.display {margin-left: 3.2em}
|
---|
40 | div.example {margin-left: 3.2em}
|
---|
41 | div.lisp {margin-left: 3.2em}
|
---|
42 | kbd {font-style: oblique}
|
---|
43 | pre.display {font-family: inherit}
|
---|
44 | pre.format {font-family: inherit}
|
---|
45 | pre.menu-comment {font-family: serif}
|
---|
46 | pre.menu-preformatted {font-family: serif}
|
---|
47 | span.nolinebreak {white-space: nowrap}
|
---|
48 | span.roman {font-family: initial; font-weight: normal}
|
---|
49 | span.sansserif {font-family: sans-serif; font-weight: normal}
|
---|
50 | ul.no-bullet {list-style: none}
|
---|
51 | -->
|
---|
52 | </style>
|
---|
53 |
|
---|
54 |
|
---|
55 | </head>
|
---|
56 |
|
---|
57 | <body lang="en">
|
---|
58 | <span id="Accessing-Array-Elements"></span><div class="header">
|
---|
59 | <p>
|
---|
60 | Next: <a href="Declaring-an-Array.html" accesskey="n" rel="next">Declaring an Array</a>, Up: <a href="Arrays.html" accesskey="u" rel="up">Arrays</a> [<a href="index.html#SEC_Contents" title="Table of contents" rel="contents">Contents</a>][<a href="Symbol-Index.html" title="Index" rel="index">Index</a>]</p>
|
---|
61 | </div>
|
---|
62 | <hr>
|
---|
63 | <span id="Accessing-Array-Elements-1"></span><h3 class="section">16.1 Accessing Array Elements</h3>
|
---|
64 | <span id="index-accessing-array-elements"></span>
|
---|
65 | <span id="index-array-elements_002c-accessing"></span>
|
---|
66 |
|
---|
67 | <p>If the variable <code>a</code> is an array, the <var>n</var>th element of
|
---|
68 | <code>a</code> is <code>a[<var>n</var>]</code>. You can use that expression to access
|
---|
69 | an element’s value or to assign to it:
|
---|
70 | </p>
|
---|
71 | <div class="example">
|
---|
72 | <pre class="example">x = a[5];
|
---|
73 | a[6] = 1;
|
---|
74 | </pre></div>
|
---|
75 |
|
---|
76 | <p>Since the variable <code>a</code> is an lvalue, <code>a[<var>n</var>]</code> is also an
|
---|
77 | lvalue.
|
---|
78 | </p>
|
---|
79 | <p>The lowest valid index in an array is 0, <em>not</em> 1, and the highest
|
---|
80 | valid index is one less than the number of elements.
|
---|
81 | </p>
|
---|
82 | <p>The C language does not check whether array indices are in bounds, so
|
---|
83 | if the code uses an out-of-range index, it will access memory outside the
|
---|
84 | array.
|
---|
85 | </p>
|
---|
86 | <p><strong>Warning:</strong> Using only valid index values in C is the
|
---|
87 | programmer’s responsibility.
|
---|
88 | </p>
|
---|
89 | <p>Array indexing in C is not a primitive operation: it is defined in
|
---|
90 | terms of pointer arithmetic and dereferencing. Now that we know
|
---|
91 | <em>what</em> <code>a[i]</code> does, we can ask <em>how</em> <code>a[i]</code> does
|
---|
92 | its job.
|
---|
93 | </p>
|
---|
94 | <p>In C, <code><var>x</var>[<var>y</var>]</code> is an abbreviation for
|
---|
95 | <code>*(<var>x</var>+<var>y</var>)</code>. Thus, <code>a[i]</code> really means
|
---|
96 | <code>*(a+i)</code>. See <a href="Pointers-and-Arrays.html">Pointers and Arrays</a>.
|
---|
97 | </p>
|
---|
98 | <p>When an expression with array type (such as <code>a</code>) appears as part
|
---|
99 | of a larger C expression, it is converted automatically to a pointer
|
---|
100 | to element zero of that array. For instance, <code>a</code> in an
|
---|
101 | expression is equivalent to <code>&a[0]</code>. Thus, <code>*(a+i)</code> is
|
---|
102 | computed as <code>*(&a[0]+i)</code>.
|
---|
103 | </p>
|
---|
104 | <p>Now we can analyze how that expression gives us the desired element of
|
---|
105 | the array. It makes a pointer to element 0 of <code>a</code>, advances it
|
---|
106 | by the value of <code>i</code>, and dereferences that pointer.
|
---|
107 | </p>
|
---|
108 | <p>Another equivalent way to write the expression is <code>(&a[0])[i]</code>.
|
---|
109 | </p>
|
---|
110 | <hr>
|
---|
111 | <div class="header">
|
---|
112 | <p>
|
---|
113 | Next: <a href="Declaring-an-Array.html" accesskey="n" rel="next">Declaring an Array</a>, Up: <a href="Arrays.html" accesskey="u" rel="up">Arrays</a> [<a href="index.html#SEC_Contents" title="Table of contents" rel="contents">Contents</a>][<a href="Symbol-Index.html" title="Index" rel="index">Index</a>]</p>
|
---|
114 | </div>
|
---|
115 |
|
---|
116 |
|
---|
117 |
|
---|
118 | </body>
|
---|
119 | </html>
|
---|