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>Variable-Length Array Parameters (GNU C Language Manual)</title>
|
---|
23 |
|
---|
24 | <meta name="description" content="Variable-Length Array Parameters (GNU C Language Manual)">
|
---|
25 | <meta name="keywords" content="Variable-Length Array Parameters (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="Advanced-Definitions.html" rel="up" title="Advanced Definitions">
|
---|
33 | <link href="Variable-Number-of-Arguments.html" rel="next" title="Variable Number of Arguments">
|
---|
34 | <link href="Advanced-Definitions.html" rel="prev" title="Advanced Definitions">
|
---|
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="Variable_002dLength-Array-Parameters"></span><div class="header">
|
---|
59 | <p>
|
---|
60 | Next: <a href="Variable-Number-of-Arguments.html" accesskey="n" rel="next">Variable Number of Arguments</a>, Up: <a href="Advanced-Definitions.html" accesskey="u" rel="up">Advanced Definitions</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="Variable_002dLength-Array-Parameters-1"></span><h4 class="subsection">22.7.1 Variable-Length Array Parameters</h4>
|
---|
64 | <span id="index-variable_002dlength-array-parameters"></span>
|
---|
65 | <span id="index-array-parameters_002c-variable_002dlength"></span>
|
---|
66 | <span id="index-functions-that-accept-variable_002dlength-arrays"></span>
|
---|
67 |
|
---|
68 | <p>An array parameter can have variable length: simply declare the array
|
---|
69 | type with a size that isn’t constant. In a nested function, the
|
---|
70 | length can refer to a variable defined in a containing scope. In any
|
---|
71 | function, it can refer to a previous parameter, like this:
|
---|
72 | </p>
|
---|
73 | <div class="example">
|
---|
74 | <pre class="example">struct entry
|
---|
75 | tester (int len, char data[len][len])
|
---|
76 | {
|
---|
77 | <span class="roman">…</span>
|
---|
78 | }
|
---|
79 | </pre></div>
|
---|
80 |
|
---|
81 | <p>Alternatively, in function declarations (but not in function
|
---|
82 | definitions), you can use <code>[*]</code> to denote that the array
|
---|
83 | parameter is of a variable length, such that these two declarations
|
---|
84 | mean the same thing:
|
---|
85 | </p>
|
---|
86 | <div class="example">
|
---|
87 | <pre class="example">struct entry
|
---|
88 | tester (int len, char data[len][len]);
|
---|
89 | </pre></div>
|
---|
90 |
|
---|
91 | <div class="example">
|
---|
92 | <pre class="example">struct entry
|
---|
93 | tester (int len, char data[*][*]);
|
---|
94 | </pre></div>
|
---|
95 |
|
---|
96 | <p>The two forms of input are equivalent in GNU C, but emphasizing that
|
---|
97 | the array parameter is variable-length may be helpful to those
|
---|
98 | studying the code.
|
---|
99 | </p>
|
---|
100 | <p>You can also omit the length parameter, and instead use some other
|
---|
101 | in-scope variable for the length in the function definition:
|
---|
102 | </p>
|
---|
103 | <div class="example">
|
---|
104 | <pre class="example">struct entry
|
---|
105 | tester (char data[*][*]);
|
---|
106 | <span class="roman">…</span>
|
---|
107 | int dataLength = 20;
|
---|
108 | <span class="roman">…</span>
|
---|
109 | struct entry
|
---|
110 | tester (char data[dataLength][dataLength])
|
---|
111 | {
|
---|
112 | <span class="roman">…</span>
|
---|
113 | }
|
---|
114 | </pre></div>
|
---|
115 |
|
---|
116 |
|
---|
117 | <span id="index-parameter-forward-declaration"></span>
|
---|
118 | <p>In GNU C, to pass the array first and the length afterward, you can
|
---|
119 | use a <em>parameter forward declaration</em>, like this:
|
---|
120 | </p>
|
---|
121 | <div class="example">
|
---|
122 | <pre class="example">struct entry
|
---|
123 | tester (int len; char data[len][len], int len)
|
---|
124 | {
|
---|
125 | <span class="roman">…</span>
|
---|
126 | }
|
---|
127 | </pre></div>
|
---|
128 |
|
---|
129 | <p>The ‘<samp>int len</samp>’ before the semicolon is the parameter forward
|
---|
130 | declaration; it serves the purpose of making the name <code>len</code> known
|
---|
131 | when the declaration of <code>data</code> is parsed.
|
---|
132 | </p>
|
---|
133 | <p>You can write any number of such parameter forward declarations in the
|
---|
134 | parameter list. They can be separated by commas or semicolons, but
|
---|
135 | the last one must end with a semicolon, which is followed by the
|
---|
136 | “real” parameter declarations. Each forward declaration must match
|
---|
137 | a subsequent “real” declaration in parameter name and data type.
|
---|
138 | </p>
|
---|
139 | <p>Standard C does not support parameter forward declarations.
|
---|
140 | </p>
|
---|
141 | <hr>
|
---|
142 | <div class="header">
|
---|
143 | <p>
|
---|
144 | Next: <a href="Variable-Number-of-Arguments.html" accesskey="n" rel="next">Variable Number of Arguments</a>, Up: <a href="Advanced-Definitions.html" accesskey="u" rel="up">Advanced Definitions</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>
|
---|
145 | </div>
|
---|
146 |
|
---|
147 |
|
---|
148 |
|
---|
149 | </body>
|
---|
150 | </html>
|
---|