Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Array as compound literal [duplicate]

In C99 we can use compound literals as unnamed array.

But are this literals constants like for example 100, 'c', 123.4f, etc.

I noticed that I can do:

((int []) {1,2,3})[0] = 100;

and, I have no compilation error and is guessable that the first element of that unnamed array is modified with 100.

So it seems as array as compound literal are lvalue and not constant value.

like image 366
xdevel2000 Avatar asked Jan 10 '23 05:01

xdevel2000


2 Answers

It is an lvalue, we can see this if we look at the draft C99 standard section 6.5.2.5 Compound literals it says (emphasis mine):

If the type name specifies an array of unknown size, the size is determined by the initializer list as specified in 6.7.8, and the type of the compound literal is that of the completed array type. Otherwise (when the type name specifies an object type), the type of the compound literal is that specified by the type name. In either case, the result is an lvalue.

If you want a const version, later on in the same section it gives the following example:

EXAMPLE 4 A read-only compound literal can be specified through constructions like:

(const float []){1e0, 1e1, 1e2, 1e3, 1e4, 1e5, 1e6}

We can find an explanation of the terminology in this Dr Dobb's article The New C: Compound Literals and says:

Compound literals are not true constants in that the value of the literal might change, as is shown later. This brings us to a bit of terminology. The C99 and C90 Standards [2, 3] use the word “constant” for tokens that represent truly unchangeable values that are impossible to modify in the language. Thus, 10 and 3.14 are an integer decimal constant and a floating constant of type double, respectively. The word “literal” is used for the representation of a value that might not be so constant. For example, early C implementations permitted the values of quoted strings to be modified. C90 and C99 banned the practice by saying that any program than modified a string literal had undefined behavior, which is the Standard’s way of saying it might work, or the program might fail in a mysterious way. [...]

like image 101
Shafik Yaghmour Avatar answered Jan 20 '23 00:01

Shafik Yaghmour


As far I remeber you are right, compound literals are lvalues*, you can also take pointer of such literal (which points to its first element):

int *p = (int []){1, 2, 3};
*p = 5; /* modified first element */

It is also possible to apply const qualifier on such compound literal, so elements are read-only:

const int *p = (const int []){1, 2, 3};
*p = 5; /* wrong, violation of `const` qualifier */

*Note this not means it's automatically modifiable lvalue (so it can used as left operand for assignment operator) since it has array type and refering to C99 draft 6.3.2.1 Lvalues, arrays, and function designators:

A modifiable lvalue is an lvalue that does not have array type, [...]

like image 22
Grzegorz Szpetkowski Avatar answered Jan 19 '23 22:01

Grzegorz Szpetkowski