In C, if it is not a permanent address, what is it?

What, in fact, are numbers in the next declaration if it is not an address constant?

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

The disassembly of the program shows that 1, 2, and 3 are dynamically placed in the local space of the stack, and not the entire array processed as a constant. Therefore, {1,2,3} does not have a static storage duration, therefore numbers not an address constant according to the C99 specification.

C99, section 6.6.9: "The address constant is a null pointer, a pointer to an lvalue, which indicates an object of static storage duration or a pointer to a function pointer ..."

However, adding the numbers++ after the declaration results in the following compilation error in GCC 4.1.2:

error: invalid lvalue in increment

So it is a permanent, but not a permanent address. Does anyone know the official name for this type of constant in C99 (or similar)?

+4
source share
5 answers

numbers is a variable automatic variable of the main function array.

Since it is automatic and volatile, it cannot have static storage.

because it is an array variable (and you will not notice the pointer), it cannot be increased.

Notice what you can do

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

You seem to be inventing something that does not really exist, terminology.

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

If your numbers were declared with a static storage duration, then the result of converting the array to pointer conversion applied to numbers will be a constant address. numbers + 1 , as well as &numbers[2] will also be a permanent address in this case.

You have the right to notice that { 1, 2, 3 } does not have a static storage duration. In fact, it does not have a shelf life at all. This is not an object, but just a piece of syntactic sugar called an aggregate initializer. If you want it to become an anonymous object, you will have to use the syntax of the compound literal: (int[]) { 1, 2, 3 } , but it will not work in the above context.

numbers++ will not compile simply because the result of the conversion between arrays and pointers is not an lvalue value. You cannot apply ++ to non-lvalue. Whether it is constant or not does not matter.

You seem to be making the strange conclusion that if you cannot change it, it must be a constant. This is completely untrue. In C terminology, the property of being constant has very little to do with the fact that it can be 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 changed. This is an example of this: the address of an automatic array is unknown at compile time, therefore, although this address is not modified, it is still not an address constant.

+10
source

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

+3
source

The reason this is not an address constant is because it is an address on the stack, and this location depends on what is above it in the call stack. Something cannot be constant if its value is unknown before runtime.

This is also not a variable, so you cannot increase it. This is simply the name of the storage location, which turns out to be an offset on the base pointer. numbers[0] is a valid value for l, but numbers not.

0
source

numbers cannot be changed since it must always point to the first element in the array.

-1
source

All Articles