C语言变长数组
<script type="text/javascript"> </script> <script type="text/javascript" src="http://pagead2.googlesyndication.com/pagead/show_ads.js"> </script>
我们知道, 传统的 C 语言是不能像C++那样支持变长数组的,也就是说数组的长度是在编译期就确定下来的,不能在运行期改变。 C99 标准定义的 C 语言新特性 ,新增的一项功能可以允许在 C 语言中使用变长数组。
C99 gives C programmers the ability to use variable length arrays, which are arrays whose sizes are not known until run time. A variable length array declaration is like a fixed array declaration except that the array size is specified by a non-constant expression. When the declaration is encountered, the size expression is evaluated and the array is created with the indicated length, which must be a positive integer. Once created, variable length array cannot change in length. Elements in the array can be accessed up to the allocated length; accessing elements beyond that length results in undefined behavior. There is no check required for such out-of-range accesses. The array is destroyed when the block containing the declaration completes. Each time the block is started, a new array is allocated.
<script type="text/javascript"> </script> <script src="http://pagead2.googlesyndication.com/pagead/show_ads.js" type="text/javascript"> </script>
以上就是 C99 标准 对 C语言 变长数组的说明.
C语变长数组,测试所用的源代码很简单,如下所示:
<script type="text/javascript"> </script> <script type="text/javascript" src="http://pagead2.googlesyndication.com/pagead/show_ads.js"> </script>
我们知道, 传统的 C 语言是不能像C++那样支持变长数组的,也就是说数组的长度是在编译期就确定下来的,不能在运行期改变。 C99 标准定义的 C 语言新特性 ,新增的一项功能可以允许在 C 语言中使用变长数组。
C99 gives C programmers the ability to use variable length arrays, which are arrays whose sizes are not known until run time. A variable length array declaration is like a fixed array declaration except that the array size is specified by a non-constant expression. When the declaration is encountered, the size expression is evaluated and the array is created with the indicated length, which must be a positive integer. Once created, variable length array cannot change in length. Elements in the array can be accessed up to the allocated length; accessing elements beyond that length results in undefined behavior. There is no check required for such out-of-range accesses. The array is destroyed when the block containing the declaration completes. Each time the block is started, a new array is allocated.
<script type="text/javascript"> </script> <script src="http://pagead2.googlesyndication.com/pagead/show_ads.js" type="text/javascript"> </script>
以上就是 C99 标准 对 C语言 变长数组的说明.
C语变长数组,测试所用的源代码很简单,如下所示:
- //文件名:dynarray.c
- //编译环境: bloodshed dev-c/c++ 4.9
- #include <stdio.h>
- #define bzero(b,len) (memset((b), '/0', (len)), (void) 0)
- int main(int argc, char *argv[])
- {
- int i, n;
- n = atoi(argv[1]);
- char arr[n+1];
- bzero(arr, (n+1) * sizeof(char));
- for (i = 0; i < n; i++) {
- arr[i] = (char)('A' + i);
- }
- arr[n] = '/0';
- printf("%s/n", arr);
- getchar();
- return (0);
- }
上述程序名为dynarray.c,其工作是把参数argv[1]的值n加上1作为变长数组arr的长度,变长数组arr的类型为char。然后向数组中写入一些字符,并将写入的字符串输出。
在支持c99标准的IDE上编译后, 在命令提示符下,运行:
c:/c99_test/dynarray.exe 6
将输出:
ABCDEF