mirror of
https://github.com/wangdoc/clang-tutorial.git
synced 2026-02-03 19:03:44 +08:00
docs(array): fixed #28
This commit is contained in:
@@ -203,11 +203,11 @@ int a[2][2] = {1, 0, 0, 2};
|
||||
数组声明的时候,数组长度除了使用常量,也可以使用变量。这叫做变长数组(variable-length array,简称 VLA)。
|
||||
|
||||
```c
|
||||
int n = a + b;
|
||||
int a[n];
|
||||
int n = x + y;
|
||||
int arr[n];
|
||||
```
|
||||
|
||||
上面示例中,数组`a`就是变长数组,因为它的长度取决于变量`n`的值,编译器没法事先确定,只有运行时才能知道`n`是多少。
|
||||
上面示例中,数组`arr`就是变长数组,因为它的长度取决于变量`n`的值,编译器没法事先确定,只有运行时才能知道`n`是多少。
|
||||
|
||||
变长数组的根本特征,就是数组长度只有运行时才能确定。它的好处是程序员不必在开发时,随意为数组指定一个估计的长度,程序可以在运行时为数组分配精确的长度。
|
||||
|
||||
|
||||
Reference in New Issue
Block a user