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>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 | <!--
|
---|
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="Arrays-of-Variable-Length"></span><div class="header">
|
---|
59 | <p>
|
---|
60 | Previous: <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> [<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
|
---|
68 | arrays, but with a length that is not a constant expression. The
|
---|
69 | storage is allocated at the point of declaration and deallocated when
|
---|
70 | the block scope containing the declaration exits. For example:
|
---|
71 | </p>
|
---|
72 | <div class="example">
|
---|
73 | <pre class="example">#include <stdio.h> /* <span class="roman">Defines <code>FILE</code>.</span> */
|
---|
74 | #include <string.h> /* <span class="roman">Declares <code>str</code>.</span> */
|
---|
75 |
|
---|
76 | FILE *
|
---|
77 | concat_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
|
---|
87 | Utilities</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
|
---|
90 | and 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’t allocate a variable-length array if the size
|
---|
94 | might be very large (more than 100,000), or in a recursive function,
|
---|
95 | because that is likely to cause stack overflow. Allocate the array
|
---|
96 | dynamically 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
|
---|
99 | storage. Jumping into the scope is not allowed; that gives an error
|
---|
100 | message.
|
---|
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
|
---|
106 | tester (int len, char data[len][len])
|
---|
107 | {
|
---|
108 | <span class="roman">…</span>
|
---|
109 | }
|
---|
110 | </pre></div>
|
---|
111 |
|
---|
112 | <p>As usual, a function argument declared with an array type
|
---|
113 | is really a pointer to an array that already exists.
|
---|
114 | Calling the function does not allocate the array, so there’s no
|
---|
115 | particular danger of stack overflow in using this construct.
|
---|
116 | </p>
|
---|
117 | <p>To pass the array first and the length afterward, use a forward
|
---|
118 | declaration in the function’s parameter list (another GNU extension).
|
---|
119 | For example,
|
---|
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 <code>int len</code> before the semicolon is a <em>parameter forward
|
---|
130 | declaration</em>, and it serves the purpose of making the name <code>len</code>
|
---|
131 | known 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 “real” declaration in parameter name and data type. ISO C11 does
|
---|
138 | not support parameter forward declarations.
|
---|
139 | </p>
|
---|
140 | <hr>
|
---|
141 | <div class="header">
|
---|
142 | <p>
|
---|
143 | Previous: <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> [<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>
|
---|