- 早期C語(yǔ)言中定義數(shù)組:
類(lèi)型說(shuō)明符 數(shù)組名 [常量表達(dá)式];
type arrayName [ arraySize ]
(The arraySize must be an integer constant greater than zero)
Arrays of Variable Length
c99引入了可變長(zhǎng)數(shù)組(variable length array,簡(jiǎn)稱(chēng)VLA);gcc編譯默認(rèn)支持新的標(biāo)準(zhǔn),也可以使用 -std=c99編譯選項(xiàng);常見(jiàn)問(wèn)題
使用可變長(zhǎng)數(shù)組時(shí)由于編譯階段還不能確定數(shù)組長(zhǎng)度,因此不能直接對(duì)其初始化,例如:
char str[len] = {0}
編譯會(huì)報(bào)錯(cuò):error: variable-sized object may not be initialized
可以通過(guò)以下類(lèi)似方式初始化:
char str[len];
memset(str, '\0', len);
另外,android studio 2.2中clang編譯一樣支持可變長(zhǎng)數(shù)組
- 參考
[1] C - Arrays
[2] Arrays of Variable Length
[3] 數(shù)組~wiki
[4] C語(yǔ)言一維數(shù)組的定義和引用