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>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 | <!--
|
---|
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="Passing-Array-Args"></span><div class="header">
|
---|
59 | <p>
|
---|
60 | Next: <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> [<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
|
---|
66 | value, like all argument values in C. However, the result is
|
---|
67 | paradoxical in that the array itself is passed by reference: its
|
---|
68 | contents are treated as shared memory—shared between the caller and
|
---|
69 | the called function, that is. When <code>clobber4</code> assigns to element
|
---|
70 | 4 of <code>array</code>, the effect is to alter element 4 of the array
|
---|
71 | specified in the call.
|
---|
72 | </p>
|
---|
73 | <div class="example">
|
---|
74 | <pre class="example">#include <stddef.h> /* <span class="roman">Defines <code>NULL</code>.</span> */
|
---|
75 | #include <stdlib.h> /* <span class="roman">Declares <code>malloc</code>,</span> */
|
---|
76 | /* <span class="roman">Defines <code>EXIT_SUCCESS</code>.</span> */
|
---|
77 |
|
---|
78 | int
|
---|
79 | main (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 < 6; i++)
|
---|
86 | printf ("data[%d] = %d\n", i, data[i]);
|
---|
87 |
|
---|
88 | printf ("\n");
|
---|
89 |
|
---|
90 | clobber4 (data);
|
---|
91 |
|
---|
92 | /* <span class="roman">Show that element 4 has been changed.</span> */
|
---|
93 | for (i = 0; i < 6; i++)
|
---|
94 | printf ("data[%d] = %d\n", i, data[i]);
|
---|
95 |
|
---|
96 | printf ("\n");
|
---|
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
|
---|
106 | whose argument type is written as <code>int [20]</code> is not an error,
|
---|
107 | because that really stands for <code>int *</code>. The pointer that is the
|
---|
108 | real argument carries no indication of the length of the array it
|
---|
109 | points into. It is not required to point to the beginning of the
|
---|
110 | array, either. For instance,
|
---|
111 | </p>
|
---|
112 | <div class="example">
|
---|
113 | <pre class="example">clobber4 (data+1);
|
---|
114 | </pre></div>
|
---|
115 |
|
---|
116 | <p>passes an “array” that starts at element 1 of <code>data</code>, and the
|
---|
117 | effect 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
|
---|
120 | size, you can specify the size of the array to be <code>static</code>:
|
---|
121 | </p>
|
---|
122 | <div class="example">
|
---|
123 | <pre class="example">void
|
---|
124 | clobber4 (int array[static 20])
|
---|
125 | <span class="roman">…</span>
|
---|
126 | </pre></div>
|
---|
127 |
|
---|
128 | <p>This is a promise to the compiler that the function will always be
|
---|
129 | called with an array of 20 elements, so that the compiler can optimize
|
---|
130 | code accordingly. If the code breaks this promise and calls the
|
---|
131 | function with, for example, a shorter array, unpredictable things may
|
---|
132 | happen.
|
---|
133 | </p>
|
---|
134 | <hr>
|
---|
135 | <div class="header">
|
---|
136 | <p>
|
---|
137 | Next: <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> [<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>
|
---|