Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

c

c99

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 local 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)?

like image 436
Carlo Avatar asked Dec 01 '22 09:12

Carlo


2 Answers

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.

like image 184
AnT Avatar answered Dec 05 '22 02:12

AnT


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;
}
like image 28
dmckee --- ex-moderator kitten Avatar answered Dec 05 '22 02:12

dmckee --- ex-moderator kitten