source: public/doc/gnu-c/Variable_002dLength-Array-Parameters.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>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<!--
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="Variable_002dLength-Array-Parameters"></span><div class="header">
59<p>
60Next: <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> &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="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
69type with a size that isn&rsquo;t constant. In a nested function, the
70length can refer to a variable defined in a containing scope. In any
71function, it can refer to a previous parameter, like this:
72</p>
73<div class="example">
74<pre class="example">struct entry
75tester (int len, char data[len][len])
76{
77 <span class="roman">&hellip;</span>
78}
79</pre></div>
80
81<p>Alternatively, in function declarations (but not in function
82definitions), you can use <code>[*]</code> to denote that the array
83parameter is of a variable length, such that these two declarations
84mean the same thing:
85</p>
86<div class="example">
87<pre class="example">struct entry
88tester (int len, char data[len][len]);
89</pre></div>
90
91<div class="example">
92<pre class="example">struct entry
93tester (int len, char data[*][*]);
94</pre></div>
95
96<p>The two forms of input are equivalent in GNU C, but emphasizing that
97the array parameter is variable-length may be helpful to those
98studying the code.
99</p>
100<p>You can also omit the length parameter, and instead use some other
101in-scope variable for the length in the function definition:
102</p>
103<div class="example">
104<pre class="example">struct entry
105tester (char data[*][*]);
106<span class="roman">&hellip;</span>
107int dataLength = 20;
108<span class="roman">&hellip;</span>
109struct entry
110tester (char data[dataLength][dataLength])
111{
112 <span class="roman">&hellip;</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
119use a <em>parameter forward declaration</em>, like this:
120</p>
121<div class="example">
122<pre class="example">struct entry
123tester (int len; char data[len][len], int len)
124{
125 <span class="roman">&hellip;</span>
126}
127</pre></div>
128
129<p>The &lsquo;<samp>int len</samp>&rsquo; before the semicolon is the parameter forward
130declaration; it serves the purpose of making the name <code>len</code> known
131when the declaration of <code>data</code> is parsed.
132</p>
133<p>You can write any number of such parameter forward declarations in the
134parameter list. They can be separated by commas or semicolons, but
135the last one must end with a semicolon, which is followed by the
136&ldquo;real&rdquo; parameter declarations. Each forward declaration must match
137a subsequent &ldquo;real&rdquo; 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>
144Next: <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> &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>
145</div>
146
147
148
149</body>
150</html>
Note: See TracBrowser for help on using the repository browser.