美文网首页
可变长数组(Arrays of Variable Length)

可变长数组(Arrays of Variable Length)

作者: smileback | 来源:发表于2017-04-09 22:24 被阅读0次
  • 早期C语言中定义数组:

类型说明符 数组名 [常量表达式];

type arrayName [ arraySize ]
(The arraySize must be an integer constant greater than zero)

  • Arrays of Variable Length
    c99引入了可变长数组(variable length array,简称VLA);gcc编译默认支持新的标准,也可以使用 -std=c99编译选项;

  • 常见问题
    使用可变长数组时由于编译阶段还不能确定数组长度,因此不能直接对其初始化,例如:
    char str[len] = {0}
    编译会报错:error: variable-sized object may not be initialized
    可以通过以下类似方式初始化:

char str[len];
memset(str, '\0', len);

另外,android studio 2.2中clang编译一样支持可变长数组

相关文章

网友评论

      本文标题:可变长数组(Arrays of Variable Length)

      本文链接:https://www.haomeiwen.com/subject/xbljattx.html