33,321
社区成员




const int a[]={1,2,3};
int main ()
{
char b[a[0]]; //lz是要这样用吗?
return 0;
}
上面的代码中,虽然a数组在只读代码段,但 a[0] 的值只有在运行时才能知道(a[0]等价于*(a+0)),在“常用”的c标准(c89)中,是非法的。const int a[3]={1,2,3};
const float f[3]={1.1,2.2,3.3};
* "a和f中的元素好像都可以改变" 这个不能在本进程(?)中改变的(g++ a[0]=1;是会报错的.
* "如果拿a的元素来做数组长度的话编译器就会报错" (不知道哪个)标准的C编译器,数组下标是不能用变量的,只读变量(const)也不行.(可能不准确)
* 在c++(g++)中,以下代码是可以编译通过的,
int main(void)
{
const int a[3]={1,2,3};
int b[a[0]];
return 0;
}
c中据我所知是有用动态分配来实现变长数组的(realloc).
struct A
{
static const int arr[10];
};
const int A::arr[10]={1,2,3};