source: public/doc/gnu-c/Array-Example-Call.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.9 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>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<!--
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="Array-Example-Call"></span><div class="header">
59<p>
60Next: <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> &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="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
66array 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">&hellip;now make use of <code>average</code>&hellip;</span> */
86}
87</pre></div>
88
89<p>This shows an array subscripting expression again, this time
90on the left side of an assignment, storing a value into an
91element 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
95space for the array, it needs to know the array&rsquo;s length. You can
96specify the length with any expression whose value is an integer, but
97in 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
100for the address of the array&rsquo;s data, and that&rsquo;s what gets passed to
101the function <code>avg_of_double</code> in <code>avg_of_double (5,
102nums_to_average)</code>.
103</p>
104<p>We can make the code easier to maintain by avoiding the need to write
1055, the array length, when calling <code>avg_of_double</code>. That way, if
106we change the array to include more elements, we won&rsquo;t have to change
107that 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
116its total size by the size of one element. See <a href="Type-Size.html">Type Size</a>, for more
117details of using <code>sizeof</code>.
118</p>
119<p>We don&rsquo;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
121more code would follow that uses that result somehow. (Why compute
122the average and not use it?) But that isn&rsquo;t part of this topic.
123</p>
124<hr>
125<div class="header">
126<p>
127Next: <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> &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>
128</div>
129
130
131
132</body>
133</html>
Note: See TracBrowser for help on using the repository browser.