开发者

In C, if this isn't an address constant, what is it?

What, exactly, is numbers in the following declaration, if it is not an address constant?

int main() {
    int numbers[3] = {1,2,3};
    return 0;
}

Disassembling the program shows that 1, 2, and 3 are dynamically placed on the lo开发者_如何学Pythoncal stack space, rather than the whole array being treated as a constant. Hence, {1,2,3} does not have static storage duration, so numbers is not an address constant, as per the C99 spec.

C99, Section 6.6.9: "An address constant is a null pointer, a pointer to an lvalue designating an object of static storage duration, or a pointer to a function designator..."

However, adding the line numbers++ after the declaration causes the following compile error in GCC 4.1.2:

error: invalid lvalue in increment

So it is constant, but isn't an address constant. Does anybody know the official name of this type of constant in C99 (or similar)?


You seem to be inventing something that isn't really there, terminology-wise.

numbers is an array. It is an automatic object of type int[3]. It cannot be used to form address constants in C, since in C address constants require objects with static storage duration.

If your numbers was declared with static storage duration, then the result of array-to-pointer conversion applied to numbers would be an address constant. numbers + 1 as well as &numbers[2] would also be address constant in that case.

You are right to note that { 1, 2, 3 } doesn't have static storage duration. In fact, it has no storage duration at all. It is not an object, but merely a piece of syntactic sugar called aggregate initializer. If you wanted it to become an anonymous object, you'd have to use the compound literal syntax: (int[]) { 1, 2, 3 }, but it wouldn't work in the above context anyway.

numbers++ will not compile simply because the result of array-to-pointer conversion is not an lvalue. You can't apply ++ to a non-lvalue. Whether something is constant or not is irrelevant.

You seem to making a strange conclusion that if you can't modify it it must be a constant. That's totally incorrect. In C terminology the property of being constant has very little to do with the property of being modifiable. The term constant refers to values that are known at compile time. Values that are not known at compile time are never called constants, even if they are not modifiable. This is an example of that: the address of an automatic array is not known at compile time, so even though that address is not modifiable, it is still not an address constant.


numbers is a non-constant, automatic, array variable of the function main.

Because it is automatic and non-constant it can not have static storage.

because it is an array variable (and not you'll notice a pointer) it can not be incremented.

Note that you can do

int main() {
    int numbers[3] = {1,2,3};
    int *n = numbers+1;
    n++;
    return 0;
}


Arrays are not pointers; see section 6 of the comp.lang.c FAQ.


The reason it's not an address constant is because it's an address on the stack, and that location depends on what is above it in the call stack. Something can't be a constant if its value isn't known until run time.

It's not a variable either, which is why you can't increment it. It's just the name of a storage location, which happens to be an offset on the base pointer. numbers[0] is a valid l-value, but numbers is not.


numbers can't be changed because it must always point to the first element in the array.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜