📄 variable-length.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.6"><!--Copyright © 1988, 1989, 1992, 1993, 1994, 1995, 1996, 1997, 1998,1999, 2000, 2001, 2002, 2003, 2004 Free Software Foundation, Inc. <p>Permission is granted to copy, distribute and/or modify this documentunder the terms of the GNU Free Documentation License, Version 1.2 orany later version published by the Free Software Foundation; with theInvariant Sections being "GNU General Public License" and "FundingFree Software", the Front-Cover texts being (a) (see below), and withthe Back-Cover Texts being (b) (see below). A copy of the license isincluded 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.--><meta http-equiv="Content-Style-Type" content="text/css"><style type="text/css"><!-- pre.display { font-family:inherit } pre.format { font-family:inherit } pre.smalldisplay { font-family:inherit; font-size:smaller } pre.smallformat { font-family:inherit; font-size:smaller } pre.smallexample { font-size:smaller } pre.smalllisp { font-size:smaller }--></style></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 anextension GCC accepts them in C89 mode and in C++. (However, GCC'simplementation of variable-length arrays does not yet conform in detailto the ISO C99 standard.) These arrays aredeclared like any other automatic arrays, but with a length that is nota constant expression. The storage is allocated at the point ofdeclaration and deallocated when the brace-level is exited. Forexample:<pre class="smallexample"> 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 thestorage. Jumping into the scope is not allowed; you get an errormessage for it. <p>You can use the function <code>alloca</code> to get an effect much likevariable-length arrays. The function <code>alloca</code> is available inmany 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 allocatedwith <code>alloca</code> exists until the containing <em>function</em> returns. The space for a variable-length array is deallocated as soon as the arrayname's scope ends. (If you use both variable-length arrays and<code>alloca</code> in the same function, deallocation of a variable-length arraywill also deallocate anything more recently allocated with <code>alloca</code>.) <p>You can also use variable-length arrays as arguments to functions:<pre class="smallexample"> 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 allocatedand 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 canuse a forward declaration in the parameter list--another GNU extension.<pre class="smallexample"> 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 forwarddeclaration</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 theparameter list. They can be separated by commas or semicolons, but thelast 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 supportparameter forward declarations. </body></html>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -