⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 variable-length.html

📁 gcc手册
💻 HTML
字号:
<html lang="en">

<head>

<title>Using the GNU Compiler Collection (GCC)</title>

<meta http-equiv="Content-Type" content="text/html">

<meta name="description" content="Using the GNU Compiler Collection (GCC)">

<meta name="generator" content="makeinfo 4.3">

<link href="http://www.gnu.org/software/texinfo/" rel="generator-home">

<!--

Copyright &copy; 1988, 1989, 1992, 1993, 1994, 1995, 1996, 1997, 1998,

1999, 2000, 2001, 2002, 2003 Free Software Foundation, Inc.



   <p>Permission is granted to copy, distribute and/or modify this document

under the terms of the GNU Free Documentation License, Version 1.2 or

any later version published by the Free Software Foundation; with the

Invariant Sections being "GNU General Public License" and "Funding

Free Software", the Front-Cover texts being (a) (see below), and with

the Back-Cover Texts being (b) (see below).  A copy of the license is

included in the section entitled "GNU Free Documentation License".



   <p>(a) The FSF's Front-Cover Text is:



   <p>A GNU Manual



   <p>(b) The FSF's Back-Cover Text is:



   <p>You have freedom to copy and modify this GNU Manual, like GNU

     software.  Copies published by the Free Software Foundation raise

     funds for GNU development.-->

</head>

<body>

<div class="node">

<p>

Node:<a name="Variable%20Length">Variable Length</a>,

Next:<a rel="next" accesskey="n" href="Empty-Structures.html#Empty%20Structures">Empty Structures</a>,

Previous:<a rel="previous" accesskey="p" href="Zero-Length.html#Zero%20Length">Zero Length</a>,

Up:<a rel="up" accesskey="u" href="C-Extensions.html#C%20Extensions">C Extensions</a>

<hr><br>

</div>



<h3 class="section">Arrays of Variable Length</h3>



   <p>Variable-length automatic arrays are allowed in ISO C99, and as an

extension GCC accepts them in C89 mode and in C++.  (However, GCC's

implementation of variable-length arrays does not yet conform in detail

to the ISO C99 standard.)  These arrays are

declared like any other automatic arrays, but with a length that is not

a constant expression.  The storage is allocated at the point of

declaration and deallocated when the brace-level is exited.  For

example:



<pre class="example">     FILE *

     concat_fopen (char *s1, char *s2, char *mode)

     {

       char str[strlen (s1) + strlen (s2) + 1];

       strcpy (str, s1);

       strcat (str, s2);

       return fopen (str, mode);

     }

     </pre>



   <p>Jumping or breaking out of the scope of the array name deallocates the

storage.  Jumping into the scope is not allowed; you get an error

message for it.



   <p>You can use the function <code>alloca</code> to get an effect much like

variable-length arrays.  The function <code>alloca</code> is available in

many other C implementations (but not in all).  On the other hand,

variable-length arrays are more elegant.



   <p>There are other differences between these two methods.  Space allocated

with <code>alloca</code> exists until the containing <em>function</em> returns. 

The space for a variable-length array is deallocated as soon as the array

name's scope ends.  (If you use both variable-length arrays and

<code>alloca</code> in the same function, deallocation of a variable-length array

will also deallocate anything more recently allocated with <code>alloca</code>.)



   <p>You can also use variable-length arrays as arguments to functions:



<pre class="example">     struct entry

     tester (int len, char data[len][len])

     {

       /* <small class="dots">...</small> */

     }

     </pre>



   <p>The length of an array is computed once when the storage is allocated

and is remembered for the scope of the array in case you access it with

<code>sizeof</code>.



   <p>If you want to pass the array first and the length afterward, you can

use a forward declaration in the parameter list--another GNU extension.



<pre class="example">     struct entry

     tester (int len; char data[len][len], int len)

     {

       /* <small class="dots">...</small> */

     }

     </pre>



   <p>The <code>int len</code> before the semicolon is a <dfn>parameter forward

declaration</dfn>, and it serves the purpose of making the name <code>len</code>

known when the declaration of <code>data</code> is parsed.



   <p>You can write any number of such parameter forward declarations in the

parameter list.  They can be separated by commas or semicolons, but the

last one must end with a semicolon, which is followed by the "real"

parameter declarations.  Each forward declaration must match a "real"

declaration in parameter name and data type.  ISO C99 does not support

parameter forward declarations.



   </body></html>



⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -