source: public/doc/gnu-c/Arrays-of-Variable-Length.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.3 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>Arrays of Variable Length (GNU C Language Manual)</title>
23
24<meta name="description" content="Arrays of Variable Length (GNU C Language Manual)">
25<meta name="keywords" content="Arrays of Variable Length (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.html" rel="up" title="Arrays">
33<link href="Enumeration-Types.html" rel="next" title="Enumeration Types">
34<link href="Constructing-Array-Values.html" rel="prev" title="Constructing Array Values">
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="Arrays-of-Variable-Length"></span><div class="header">
59<p>
60Previous: <a href="Constructing-Array-Values.html" accesskey="p" rel="prev">Constructing Array Values</a>, Up: <a href="Arrays.html" accesskey="u" rel="up">Arrays</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="Arrays-of-Variable-Length-1"></span><h3 class="section">16.9 Arrays of Variable Length</h3>
64<span id="index-array-of-variable-length"></span>
65<span id="index-variable_002dlength-arrays"></span>
66
67<p>In GNU C, you can declare variable-length arrays like any other
68arrays, but with a length that is not a constant expression. The
69storage is allocated at the point of declaration and deallocated when
70the block scope containing the declaration exits. For example:
71</p>
72<div class="example">
73<pre class="example">#include &lt;stdio.h&gt; /* <span class="roman">Defines <code>FILE</code>.</span> */
74#include &lt;string.h&gt; /* <span class="roman">Declares <code>str</code>.</span> */
75
76FILE *
77concat_fopen (char *s1, char *s2, char *mode)
78{
79 char str[strlen (s1) + strlen (s2) + 1];
80 strcpy (str, s1);
81 strcat (str, s2);
82 return fopen (str, mode);
83}
84</pre></div>
85
86<p>(This uses some standard library functions; see <a href="https://www.gnu.org/software/libc/manual/html_node/String-and-Array-Utilities.html#String-and-Array-Utilities">String and Array
87Utilities</a> in <cite>The GNU C Library Reference Manual</cite>.)
88</p>
89<p>The length of an array is computed once when the storage is allocated
90and is remembered for the scope of the array in case it is used in
91<code>sizeof</code>.
92</p>
93<p><strong>Warning:</strong> don&rsquo;t allocate a variable-length array if the size
94might be very large (more than 100,000), or in a recursive function,
95because that is likely to cause stack overflow. Allocate the array
96dynamically instead (see <a href="Dynamic-Memory-Allocation.html">Dynamic Memory Allocation</a>).
97</p>
98<p>Jumping or breaking out of the scope of the array name deallocates the
99storage. Jumping into the scope is not allowed; that gives an error
100message.
101</p>
102<p>You can also use variable-length arrays as arguments to functions:
103</p>
104<div class="example">
105<pre class="example">struct entry
106tester (int len, char data[len][len])
107{
108 <span class="roman">&hellip;</span>
109}
110</pre></div>
111
112<p>As usual, a function argument declared with an array type
113is really a pointer to an array that already exists.
114Calling the function does not allocate the array, so there&rsquo;s no
115particular danger of stack overflow in using this construct.
116</p>
117<p>To pass the array first and the length afterward, use a forward
118declaration in the function&rsquo;s parameter list (another GNU extension).
119For example,
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 <code>int len</code> before the semicolon is a <em>parameter forward
130declaration</em>, and it serves the purpose of making the name <code>len</code>
131known 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
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 &ldquo;real&rdquo; declaration in parameter name and data type. ISO C11 does
138not support parameter forward declarations.
139</p>
140<hr>
141<div class="header">
142<p>
143Previous: <a href="Constructing-Array-Values.html" accesskey="p" rel="prev">Constructing Array Values</a>, Up: <a href="Arrays.html" accesskey="u" rel="up">Arrays</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>
144</div>
145
146
147
148</body>
149</html>
Note: See TracBrowser for help on using the repository browser.