C语言 C 中的常量数组
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/32705419/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Const arrays in C
提问by sandeeps
Original question: If I define:
原始问题:如果我定义:
const int z[5] = {10, 11, 12, 13, 14};
does it mean:
是不是意味着:
- it's a constant array of integers i.e. the address which z points to is always constant and can never change, but the elements of z can change.
- 它是一个常量数组,即 z 指向的地址始终是常量并且永远不会改变,但 z 的元素可以改变。
OR
或者
- Each element of z is a constant i.e. their value can never change.
- z 的每个元素都是一个常数,即它们的值永远不会改变。
Edit:
编辑:
More info:
更多信息:
There is another variable:
还有一个变量:
const int *y = z;
func((int *) y);
where func is defined as:
其中 func 定义为:
void func(int y[]) {
int i;
for(i = 0; i < 5; i++) {
y[i] = i; //y[i] can be set to any integer; used i as example
}
}
where in func, using y, the array is traversed and each element is changed. Is this is valid even though all elements of z are const?
在 func 中,使用 y 遍历数组并更改每个元素。即使 z 的所有元素都是常量,这是否有效?
回答by Keith Thompson
It means that each element of zis read-only.
这意味着 的每个元素z都是只读的。
The object zis an array object, not a pointer object; it doesn't point to anything. Like any object, the address of zdoes not change during its lifetime.
对象z是数组对象,不是指针对象;它不指向任何东西。与任何对象一样,的地址z在其生命周期内不会改变。
Since the objectzis an array, the expressionz, in most but not all contexts, is implicitly converted to a pointer expression, pointing to z[0]. That address, like the address of the entire array object z, doesn't change during the object's lifetime. This "conversion" is a compile-time adjustment to the meaning of the expression, not a run-time type conversion.
由于对象z是一个阵列,所述表达z,在大多数但不是所有的上下文中,被隐式转换为指针表达式,指向z[0]。该地址,就像整个数组对象的地址一样z,在对象的生命周期内不会改变。这种“转换”是对表达式含义的编译时调整,而不是运行时类型转换。
To understand the (often confusing) relationship between arrays and pointers, read section 6 of the comp.lang.c FAQ.
要了解数组和指针之间(通常令人困惑)的关系,请阅读comp.lang.c FAQ 的第 6 节。
It's important to understand that "constant" and constare two different things. If something is constant, it's evaluated at compile time; for example, 42and (2+2)are constant expressions.
重要的是要理解“常数”const是两个不同的东西。如果某些东西是constant,则在编译时对其进行评估;例如,42和(2+2)是常量表达式。
If an object is defined with the constkeyword, that means that it's read-only, not (necessarily) that it's constant. It means that you can't attempt to modify the object via its name, and attempting to modify it by other means (say, by taking its address and casting to a non-const pointer) has undefined behavior. Note, for example, that this:
如果一个对象是用const关键字定义的,这意味着它是只读的,而不是(必然)它是常量。这意味着您不能尝试通过名称修改对象,并且尝试通过其他方式修改它(例如,通过获取其地址并将其转换为非常量指针)具有未定义的行为。例如,请注意:
const int r = rand();
is valid. ris read-only, but its value cannot be determined until run time.
已验证。r是只读的,但它的值直到运行时才能确定。
回答by Ziezi
In your case the answer is:
在你的情况下,答案是:
- Each element of z is a constant i.e. their value can never change.
- z 的每个元素都是一个常数,即它们的值永远不会改变。
You can't create a constarray because arrays are objects and can only be created at runtime and constentities are resolved at compile time.
不能创建const数组,因为数组是对象,只能在运行时创建,const实体在编译时解析。
So, the constis interpreted as in the first example below, i.e. applied for the elements of the array. Which means that the following are equivalent:
The array in your example needs to be initialized.
因此,const被解释为在下面的第一个示例中,即应用于数组的元素。这意味着以下内容是等效的:
您的示例中的数组需要初始化。
int const z[5] = { /*initial (and only) values*/};
const int z[5] = { /*-//-*/ };
It is some type commutative propertyof the constspecifier and the type-specifier, in your example int.
这是某种类型的交换律的的const符和类型说明符,在你的榜样int。
Here are few examples to clarify the usage of constant:
下面举几个例子来说明常量的用法:
1.Constant integers definition: (can not be reassigned). In the below two expression the use of constis equivalent:
1.常量整数定义:(不能重新赋值)。在下面的两个表达式中,使用const是等效的:
int const a = 3; // after type identifier
const int b = 4; // equivalent to before type qualifier
2.Constant pointer definition (no pointer arithmetics or reassignment allowed):
2.常量指针定义(不允许指针运算或重新赋值):
int * const p = &anInteger; // non-constant data, constant pointer
and pointer definition to a constant int(the value of the pointed integer cannot be changed, but the pointer can):
和指向常量的指针定义int(指向的整数的值不能更改,但指针可以):
const int *p = &anInteger; // constant data, non-constant pointer

