source: public/doc/gnu-c/Accessing-Array-Elements.html@ 02598c2

Last change on this file since 02598c2 was 02598c2, checked in by Mikhail Kirillov <w96k@…>, on Oct 6, 2022 at 12:36:29 PM

Add gnu-c

  • Property mode set to 100644
File size: 5.2 KB
Line 
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
6licensed to the FSF.)
7
8Permission is granted to copy, distribute and/or modify this document
9under the terms of the GNU Free Documentation License, Version 1.3 or
10any later version published by the Free Software Foundation; with the
11Invariant Sections being "GNU General Public License," with the
12Front-Cover Texts being "A GNU Manual," and with the Back-Cover
13Texts as in (a) below. A copy of the license is included in the
14section entitled "GNU Free Documentation License."
15
16(a) The FSF's Back-Cover Text is: "You have the freedom to copy and
17modify this GNU manual. Buying copies from the FSF supports it in
18developing 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<!--
37a.summary-letter {text-decoration: none}
38blockquote.indentedblock {margin-right: 0em}
39div.display {margin-left: 3.2em}
40div.example {margin-left: 3.2em}
41div.lisp {margin-left: 3.2em}
42kbd {font-style: oblique}
43pre.display {font-family: inherit}
44pre.format {font-family: inherit}
45pre.menu-comment {font-family: serif}
46pre.menu-preformatted {font-family: serif}
47span.nolinebreak {white-space: nowrap}
48span.roman {font-family: initial; font-weight: normal}
49span.sansserif {font-family: sans-serif; font-weight: normal}
50ul.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>
60Next: <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> &nbsp; [<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
69an element&rsquo;s value or to assign to it:
70</p>
71<div class="example">
72<pre class="example">x = a[5];
73a[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
77lvalue.
78</p>
79<p>The lowest valid index in an array is 0, <em>not</em> 1, and the highest
80valid 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
83if the code uses an out-of-range index, it will access memory outside the
84array.
85</p>
86<p><strong>Warning:</strong> Using only valid index values in C is the
87programmer&rsquo;s responsibility.
88</p>
89<p>Array indexing in C is not a primitive operation: it is defined in
90terms 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
92its 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
99of a larger C expression, it is converted automatically to a pointer
100to element zero of that array. For instance, <code>a</code> in an
101expression is equivalent to <code>&amp;a[0]</code>. Thus, <code>*(a+i)</code> is
102computed as <code>*(&amp;a[0]+i)</code>.
103</p>
104<p>Now we can analyze how that expression gives us the desired element of
105the array. It makes a pointer to element 0 of <code>a</code>, advances it
106by the value of <code>i</code>, and dereferences that pointer.
107</p>
108<p>Another equivalent way to write the expression is <code>(&amp;a[0])[i]</code>.
109</p>
110<hr>
111<div class="header">
112<p>
113Next: <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> &nbsp; [<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>
Note: See TracBrowser for help on using the repository browser.