source: public/doc/gnu-c/Passing-Array-Args.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: 6.0 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>Passing Array Args (GNU C Language Manual)</title>
23
24<meta name="description" content="Passing Array Args (GNU C Language Manual)">
25<meta name="keywords" content="Passing Array Args (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-as-Parameters.html" rel="up" title="Arrays as Parameters">
33<link href="Array-Parm-Qualifiers.html" rel="next" title="Array Parm Qualifiers">
34<link href="Array-Parm-Pointer.html" rel="prev" title="Array Parm Pointer">
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="Passing-Array-Args"></span><div class="header">
59<p>
60Next: <a href="Array-Parm-Qualifiers.html" accesskey="n" rel="next">Array Parm Qualifiers</a>, Previous: <a href="Array-Parm-Pointer.html" accesskey="p" rel="prev">Array Parm Pointer</a>, Up: <a href="Arrays-as-Parameters.html" accesskey="u" rel="up">Arrays as Parameters</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="Passing-array-arguments"></span><h4 class="subsubsection">22.1.4.2 Passing array arguments</h4>
64
65<p>The function call passes this pointer by
66value, like all argument values in C. However, the result is
67paradoxical in that the array itself is passed by reference: its
68contents are treated as shared memory&mdash;shared between the caller and
69the called function, that is. When <code>clobber4</code> assigns to element
704 of <code>array</code>, the effect is to alter element 4 of the array
71specified in the call.
72</p>
73<div class="example">
74<pre class="example">#include &lt;stddef.h&gt; /* <span class="roman">Defines <code>NULL</code>.</span> */
75#include &lt;stdlib.h&gt; /* <span class="roman">Declares <code>malloc</code>,</span> */
76 /* <span class="roman">Defines <code>EXIT_SUCCESS</code>.</span> */
77
78int
79main (void)
80{
81 int data[] = {1, 2, 3, 4, 5, 6};
82 int i;
83
84 /* <span class="roman">Show the initial value of element 4.</span> */
85 for (i = 0; i &lt; 6; i++)
86 printf (&quot;data[%d] = %d\n&quot;, i, data[i]);
87
88 printf (&quot;\n&quot;);
89
90 clobber4 (data);
91
92 /* <span class="roman">Show that element 4 has been changed.</span> */
93 for (i = 0; i &lt; 6; i++)
94 printf (&quot;data[%d] = %d\n&quot;, i, data[i]);
95
96 printf (&quot;\n&quot;);
97
98 return EXIT_SUCCESS;
99}
100</pre></div>
101
102<p>shows that <code>data[4]</code> has become zero after the call to
103<code>clobber4</code>.
104</p>
105<p>The array <code>data</code> has 6 elements, but passing it to a function
106whose argument type is written as <code>int [20]</code> is not an error,
107because that really stands for <code>int *</code>. The pointer that is the
108real argument carries no indication of the length of the array it
109points into. It is not required to point to the beginning of the
110array, either. For instance,
111</p>
112<div class="example">
113<pre class="example">clobber4 (data+1);
114</pre></div>
115
116<p>passes an &ldquo;array&rdquo; that starts at element 1 of <code>data</code>, and the
117effect is to zero <code>data[5]</code> instead of <code>data[4]</code>.
118</p>
119<p>If all calls to the function will provide an array of a particular
120size, you can specify the size of the array to be <code>static</code>:
121</p>
122<div class="example">
123<pre class="example">void
124clobber4 (int array[static 20])
125<span class="roman">&hellip;</span>
126</pre></div>
127
128<p>This is a promise to the compiler that the function will always be
129called with an array of 20 elements, so that the compiler can optimize
130code accordingly. If the code breaks this promise and calls the
131function with, for example, a shorter array, unpredictable things may
132happen.
133</p>
134<hr>
135<div class="header">
136<p>
137Next: <a href="Array-Parm-Qualifiers.html" accesskey="n" rel="next">Array Parm Qualifiers</a>, Previous: <a href="Array-Parm-Pointer.html" accesskey="p" rel="prev">Array Parm Pointer</a>, Up: <a href="Arrays-as-Parameters.html" accesskey="u" rel="up">Arrays as Parameters</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>
138</div>
139
140
141
142</body>
143</html>
Note: See TracBrowser for help on using the repository browser.