在C文件范围内动态可变数组
问题描述:
我有一些code是这样的:
I have some code like this:
static int a = 6;
static int b = 3;
static int Hello[a][b] =
{
{ 1,2,3},
{ 1,2,3},
{ 1,2,3},
{ 1,2,3},
{ 1,2,3},
{ 1,2,3}
};
但是当我编译它,它说错误:
but when I compile it, it says error:
在文件范围内动态可变你好
variably modified 'Hello' at file scope
怎么会发生这种事?我怎么能解决这个问题?
how could this happen? and how could I fix it?
答
您不能有静态数组的大小而给出一个变量
You can not have static array which size is given as a variable
这就是为什么常量应的#define
D:
That's why constants should be #define
d:
#define a 6
此方法preprocessor将取代 A
与 6
,使其成为有效的申报。
This way preprocessor will replace a
with 6
, making it valid declaration.