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>Array Example Call (GNU C Language Manual)</title>
|
---|
23 |
|
---|
24 | <meta name="description" content="Array Example Call (GNU C Language Manual)">
|
---|
25 | <meta name="keywords" content="Array Example Call (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="Beyond-Integers.html" rel="up" title="Beyond Integers">
|
---|
33 | <link href="Array-Example-Variations.html" rel="next" title="Array Example Variations">
|
---|
34 | <link href="Array-Example.html" rel="prev" title="Array Example">
|
---|
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="Array-Example-Call"></span><div class="header">
|
---|
59 | <p>
|
---|
60 | Next: <a href="Array-Example-Variations.html" accesskey="n" rel="next">Array Example Variations</a>, Previous: <a href="Array-Example.html" accesskey="p" rel="prev">Array Example</a>, Up: <a href="Beyond-Integers.html" accesskey="u" rel="up">Beyond Integers</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="Calling-the-Array-Example"></span><h3 class="section">4.3 Calling the Array Example</h3>
|
---|
64 |
|
---|
65 | <p>To call the function <code>avg_of_double</code> requires making an
|
---|
66 | array and then passing it as an argument. Here is an example.
|
---|
67 | </p>
|
---|
68 | <div class="example">
|
---|
69 | <pre class="example">{
|
---|
70 | /* <span class="roman">The array of values to average.</span> */
|
---|
71 | double nums_to_average[5];
|
---|
72 | /* <span class="roman">The average, once we compute it.</span> */
|
---|
73 | double average;
|
---|
74 |
|
---|
75 | /* <span class="roman">Fill in elements of <code>nums_to_average</code>.</span> */
|
---|
76 |
|
---|
77 | nums_to_average[0] = 58.7;
|
---|
78 | nums_to_average[1] = 5.1;
|
---|
79 | nums_to_average[2] = 7.7;
|
---|
80 | nums_to_average[3] = 105.2;
|
---|
81 | nums_to_average[4] = -3.14159;
|
---|
82 |
|
---|
83 | average = avg_of_double (5, nums_to_average);
|
---|
84 |
|
---|
85 | /* <span class="roman">…now make use of <code>average</code>…</span> */
|
---|
86 | }
|
---|
87 | </pre></div>
|
---|
88 |
|
---|
89 | <p>This shows an array subscripting expression again, this time
|
---|
90 | on the left side of an assignment, storing a value into an
|
---|
91 | element of an array.
|
---|
92 | </p>
|
---|
93 | <p>It also shows how to declare a local variable that is an array:
|
---|
94 | <code>double nums_to_average[5];</code>. Since this declaration allocates the
|
---|
95 | space for the array, it needs to know the array’s length. You can
|
---|
96 | specify the length with any expression whose value is an integer, but
|
---|
97 | in this declaration the length is a constant, the integer 5.
|
---|
98 | </p>
|
---|
99 | <p>The name of the array, when used by itself as an expression, stands
|
---|
100 | for the address of the array’s data, and that’s what gets passed to
|
---|
101 | the function <code>avg_of_double</code> in <code>avg_of_double (5,
|
---|
102 | nums_to_average)</code>.
|
---|
103 | </p>
|
---|
104 | <p>We can make the code easier to maintain by avoiding the need to write
|
---|
105 | 5, the array length, when calling <code>avg_of_double</code>. That way, if
|
---|
106 | we change the array to include more elements, we won’t have to change
|
---|
107 | that call. One way to do this is with the <code>sizeof</code> operator:
|
---|
108 | </p>
|
---|
109 | <div class="example">
|
---|
110 | <pre class="example"> average = avg_of_double ((sizeof (nums_to_average)
|
---|
111 | / sizeof (nums_to_average[0])),
|
---|
112 | nums_to_average);
|
---|
113 | </pre></div>
|
---|
114 |
|
---|
115 | <p>This computes the number of elements in <code>nums_to_average</code> by dividing
|
---|
116 | its total size by the size of one element. See <a href="Type-Size.html">Type Size</a>, for more
|
---|
117 | details of using <code>sizeof</code>.
|
---|
118 | </p>
|
---|
119 | <p>We don’t show in this example what happens after storing the result of
|
---|
120 | <code>avg_of_double</code> in the variable <code>average</code>. Presumably
|
---|
121 | more code would follow that uses that result somehow. (Why compute
|
---|
122 | the average and not use it?) But that isn’t part of this topic.
|
---|
123 | </p>
|
---|
124 | <hr>
|
---|
125 | <div class="header">
|
---|
126 | <p>
|
---|
127 | Next: <a href="Array-Example-Variations.html" accesskey="n" rel="next">Array Example Variations</a>, Previous: <a href="Array-Example.html" accesskey="p" rel="prev">Array Example</a>, Up: <a href="Beyond-Integers.html" accesskey="u" rel="up">Beyond Integers</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>
|
---|
128 | </div>
|
---|
129 |
|
---|
130 |
|
---|
131 |
|
---|
132 | </body>
|
---|
133 | </html>
|
---|