This question already has an answer here:
这个问题在这里已有答案:
- Can a const variable be used to declare the size of an array in C? 5 answers
可以使用const变量来声明C中数组的大小吗? 5个答案
In the following code, const int cannot be used as an array size:
在以下代码中,const int不能用作数组大小:
const int sz = 0;
typedef struct
{
char s[sz];
} st;
int main()
{
st obj;
strcpy(obj.s, "hello world");
printf("%s", obj.s);
return 0;
}
3 个解决方案
#1
11
In C, a const
-qualified variable is not a constant expression1. A constant expression is something that can be evaluated at compile time - a numeric literal like 10
or 3.14159
, a string literal like "Hello"
, a sizeof
expression, or some expression made up of the same like 10 + sizeof "Hello"
.
在C中,const限定变量不是常量expression1。常量表达式可以在编译时计算 - 数字文字如10或3.14159,字符串文字如“Hello”,sizeof表达式,或某些表达式由10 + sizeof“Hello”组成。
For array declarations at file scope (outside the body of any function) or as members of struct
or union
types, the array dimension must be a constant expression.
对于文件范围(在任何函数体外)的数组声明或作为struct或union类型的成员,数组维必须是常量表达式。
For auto
arrays (arrays declared within the body of a function that are not static
), you can use a variable or expression whose value isn't known until runtime, but only in C99 or later.
对于自动数组(在函数体内声明的非静态数组),可以使用变量或表达式,其值在运行时之前是未知的,但仅限于C99或更高版本。
- C++ is different in this regard - in that language, a
const
-qualified variable does count as a constant expression.
C ++在这方面有所不同 - 在该语言中,const限定变量确实计为常量表达式。
#2
10
This is because in C const
actually means read only. Quoting C FAQ 1.18 and 1.19:
这是因为在C const中实际上意味着只读。引用C FAQ 1.18和1.19:
The const qualifier really means ``read-only''; an object so qualified is a run-time object which cannot (normally) be assigned to. The value of a const-qualified object is therefore not a constant expression in the full sense of the term, and cannot be used for array dimensions, case labels, and the like. (C is unlike C++ in this regard.) When you need a true compile-time constant, use a preprocessor #define (or perhaps an enum).
const限定符实际上意味着“只读”;一个如此限定的对象是一个不能(通常)分配给它的运行时对象。因此,const限定对象的值在该术语的完整意义上不是常量表达式,并且不能用于数组维度,案例标签等。 (在这方面,C与C ++不同。)当您需要真正的编译时常量时,请使用预处理器#define(或者可能是枚举)。
References: ISO Sec. 6.4 H&S Secs. 7.11.2,7.11.3 pp. 226-7
参考文献:ISO Sec。 6.4 H&S Secs。 7.11.2,7.11.3第226-7页
There are two ways of dealing with it:
处理它有两种方法:
- Use
#define
instead ofconst
- Use
enum { sz = 12 };
使用#define而不是const
使用enum {sz = 12};
#3
3
In a very simple way because the compiler must know the dimension of the array at compilation time and since you can initializate const variable
at run time you can't do it. So the size of statically bounded arrays must be a constant expression and a const variable
is not it. For constant expression you should use : #define
or enum
. That's explicitly for you case (at file scope) and if you use a minimum standard of c99
以一种非常简单的方式,因为编译器必须在编译时知道数组的维度,并且因为你可以在运行时初始化const变量,所以你不能这样做。因此,静态有界数组的大小必须是常量表达式,而const变量不是它。对于常量表达式,您应该使用:#define或enum。这明确适用于您的情况(在文件范围内)并且如果您使用最低标准c99
#1
11
In C, a const
-qualified variable is not a constant expression1. A constant expression is something that can be evaluated at compile time - a numeric literal like 10
or 3.14159
, a string literal like "Hello"
, a sizeof
expression, or some expression made up of the same like 10 + sizeof "Hello"
.
在C中,const限定变量不是常量expression1。常量表达式可以在编译时计算 - 数字文字如10或3.14159,字符串文字如“Hello”,sizeof表达式,或某些表达式由10 + sizeof“Hello”组成。
For array declarations at file scope (outside the body of any function) or as members of struct
or union
types, the array dimension must be a constant expression.
对于文件范围(在任何函数体外)的数组声明或作为struct或union类型的成员,数组维必须是常量表达式。
For auto
arrays (arrays declared within the body of a function that are not static
), you can use a variable or expression whose value isn't known until runtime, but only in C99 or later.
对于自动数组(在函数体内声明的非静态数组),可以使用变量或表达式,其值在运行时之前是未知的,但仅限于C99或更高版本。
- C++ is different in this regard - in that language, a
const
-qualified variable does count as a constant expression.
C ++在这方面有所不同 - 在该语言中,const限定变量确实计为常量表达式。
#2
10
This is because in C const
actually means read only. Quoting C FAQ 1.18 and 1.19:
这是因为在C const中实际上意味着只读。引用C FAQ 1.18和1.19:
The const qualifier really means ``read-only''; an object so qualified is a run-time object which cannot (normally) be assigned to. The value of a const-qualified object is therefore not a constant expression in the full sense of the term, and cannot be used for array dimensions, case labels, and the like. (C is unlike C++ in this regard.) When you need a true compile-time constant, use a preprocessor #define (or perhaps an enum).
const限定符实际上意味着“只读”;一个如此限定的对象是一个不能(通常)分配给它的运行时对象。因此,const限定对象的值在该术语的完整意义上不是常量表达式,并且不能用于数组维度,案例标签等。 (在这方面,C与C ++不同。)当您需要真正的编译时常量时,请使用预处理器#define(或者可能是枚举)。
References: ISO Sec. 6.4 H&S Secs. 7.11.2,7.11.3 pp. 226-7
参考文献:ISO Sec。 6.4 H&S Secs。 7.11.2,7.11.3第226-7页
There are two ways of dealing with it:
处理它有两种方法:
- Use
#define
instead ofconst
- Use
enum { sz = 12 };
使用#define而不是const
使用enum {sz = 12};
#3
3
In a very simple way because the compiler must know the dimension of the array at compilation time and since you can initializate const variable
at run time you can't do it. So the size of statically bounded arrays must be a constant expression and a const variable
is not it. For constant expression you should use : #define
or enum
. That's explicitly for you case (at file scope) and if you use a minimum standard of c99
以一种非常简单的方式,因为编译器必须在编译时知道数组的维度,并且因为你可以在运行时初始化const变量,所以你不能这样做。因此,静态有界数组的大小必须是常量表达式,而const变量不是它。对于常量表达式,您应该使用:#define或enum。这明确适用于您的情况(在文件范围内)并且如果您使用最低标准c99