Array as compound literal [duplicate] - c

This question already has answers here:
Why are compound literals in C modifiable
(2 answers)
Closed 4 years ago.
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.

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. [...]

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, [...]

Referring to the C11 standard draft N1570:
Section 6.5.2.5p4:
In either case, the result is an lvalue.
An "lvalue" is, roughly, an expression that designates an object -- but it's important to note that not all lvalues are modifiable. A simple example:
const int x = 42;
The name x is an lvalue, but it's not a modifiable lvalue. (Expressions of array type cannot be modifiable lvalues, because you can't assign to an array object, but the elements of an array may be modifiable.)
Paragraph 5 of the same section:
The value of the compound literal is that of an unnamed object
initialized by the initializer list. If the compound literal occurs
outside the body of a function, the object has static storage
duration; otherwise, it has automatic storage duration associated with
the enclosing block.
The section describing compound literals doesn't specifically say that whether the unnamed object is modifiable or not. In the absence of such a statement, the object is taken to be modifiable unless the type is const-qualified.
The example in the question:
((int []) {1,2,3})[0] = 100;
is not particularly useful, since there's no way to refer to the unnamed object after the assignment. But a similar construct can be quite useful. A contrived example:
#include <stdio.h>
int main(void) {
int *ptr = (int[]){1, 2, 3};
ptr[0] = 100;
printf("%d %d %d\n", ptr[0], ptr[1], ptr[2]);
}
As mentioned above, the array has automatic storage duration, which means that if it's created inside a function, it will cease to exist when the function returns. Compound literals are not a replacement for malloc.

Compound literals are lvalues and it's elements can be modifiable. You can assign value to it. Even pointer to compound literals are allowed.

Related

Postfix expression (type-name){initializer-list}

The section 6.5.2.5/4 provides an explanation about the postfix-expression of the form ( type-name ) { initializer-list }. Here it is:
If the type name specifies an array of unknown size, the size is
determined by the initializer list as specified in 6.7.9, 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.
I don't understand the wording the type of the compound literal. How is it even possible for a literal to have a type? Does the type of the corresponding unnamed object is meant by the type of the compound literal?
For instance
long long *longs = (long long []) {1333, 3123, 3, 122};
The initializer-list here is used to initialize an unnamed object of type long long [4].
Also it is not clear what is the purpose of In either case, the result is an lvalue. When using in an assignment-expression the lvalue conversion is performed on the right operand so it is no longer an lvalue.
Presumably, a "compound literal" means the object/value that a use of the "compound literal" language construct designates. Values/objects have types in C.
The purpose of making compound literals lvalues is so that code like
int *x = &(int){42}; *x = 43; works. It makes compound literals behave kind of like anonymous variables. (Not completely, though. Unlike regular variables, compound literals can't have storage class specifiers, which I personally consider a language defect.)
The main purpose of compound literal is to provide an anonymous object, so you can have a pointer to an real object without creating a variable first.
It's an lvalue, which means that one may obtain it address. But, it's not modifiable lvalue though, just like an ordinary array object:
int a[] = {0, 0};
a = {1, 2}; // error, a is not a modifiable lvalue
But of course, you can modify it by using subscript operator or indirect pointer access:
a[0] = 1; // fine
(long long []) {1333, 3123, 3, 122}[0] = 1; // fine as well

C - initializer not constant with string literal

I am aware of the limitations for initializers at file-scope in c: you cannot use a variable (even const), functions etc... But I really do not understand why this does not work:
#include <stdlib.h>
#include <stdio.h>
//unsigned long a = "string"[0] > '0'; // this does not compile
unsigned long a = 's' > '0'; // this works fine, output is "a = 1"
int main(void)
{
printf("a = %lu\n",a);
return 0;
}
Why does the line with the string literal gives: error: initializer element is not constant. Are string literals not considered constant? Is there any way to make it work?
Thanks in advance
Your variable has static storage duration, as such and according to N1570 (C11) §6.7.9/p4:
All the expressions in an initializer for an object that has static or
thread storage duration shall be constant expressions or string
literals.
String literals have static storage duration §6.4.5/p6, so their addresses can be considered constant expressions (which is why they are allowed as initializers). But you are trying to access the value at such an address, and the C standard explicitly forbids it. To quote §6.6/p9, emphasis mine:
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; it shall be created explicitly using the unary &
operator or an integer constant cast to pointer type, or implicitly by
the use of an expression of array or function type. The
array-subscript [] and member-access . and -> operators, the address &
and indirection * unary operators, and pointer casts may be used in
the creation of an address constant, but the value of an object shall
not be accessed by use of these operators.
On the other hand, when you used character constants for comparison, you obtain a valid constant expression.
In C language objects with static storage duration have to be initialized with constant expressions or with aggregate initializers containing constant expressions.
Now here you had basically "string"[0] as an constant expression? But is it so?
The thing is From 6.6p2
A constant expression can be evaluated during translation rather than runtime, and accordingly may be used in any place that a constant may be.
Now after that I checked the translation phases and what it consists of:
It will be clear that none of the expression is evaluated which involves a [] array subscripting over string literal. This literally involves dereferencing an address and getting the vlaue which is not possible to do in translation phases. That's why this error.

Why is not possible to assign a pointer to an array? [duplicate]

This question already has answers here:
Why can´t we assign a new string to an char array, but to a pointer?
(4 answers)
Closed 6 years ago.
In C, I'm coding this
char * real = strdup("GEORGE");
char one[1024];
one = real;
and it gives error:
invalid initializer
any suggestions?
is there any chance I can make array of chars equal to char pointer?
In your code, one is a variable of type array. Thus,
one = real;
is attempt to assign to an array type, which is not allowed.
To elaborate, array names are no modifiable lvalues and assignment operator only works on modifiable lvalues as the LHS operand.
Quoting C11, chapter §6.5.16
An assignment operator shall have a modifiable lvalue as its left operand.
and then, chapter §6.3.2.1, (emphais mine)
A modifiable lvalue is an lvalue that
does not have array type, does not have an incomplete type, does not have a const qualified
type, and if it is a structure or union, does not have any member (including,
recursively, any member or element of all contained aggregates or unions) with a const qualified
type.
You need to use strcpy() to copy the content to the array.
C requires constants in array initializers. You are allowed to do this:
char one[1024] = "GEORGE";
or this
char one[1024] = {'G','E','O','R','G','E'};
but assigning a pointer to an array is not allowed under any circumstances, initializer or not.
On the other hand, you can copy a content of a char pointer into an array. Depending on whether your source array is null-terminated or not, you can use strcpy or memcpy, like this:
strcpy(one, real);
or
memcpy(one, real, 7);

Compound literals for scalar types

Since c99 compound literals can be used to e.g. initialize pointers:
int *p = (int []) {1, 2, 3, 4};
while this is usually used for structs it can be used to initialize anonymous arrays as well (see example). But if I understood this correctly initializing a scalar pointer like this:
int *p = &(int) {4};
is also valid. What I find confusing is the statement on the gcc site that “Compound literals for scalar types and union types are also allowed, but then the compound literal is equivalent to a cast.” Do they simply mean that using the address-of operator & in front of an anonymous scalar is a form of casting?
By definition a compound literal does not consist of & address-of operator. From N1570 6.5.2.5/p3 Compound literals:
A postfix expression that consists of a parenthesized type name
followed by a brace-enclosed list of initializers is a compound
literal.
Now, their statement:
Compound literals for scalar types and union types are also allowed,
but then the compound literal is equivalent to a cast.
is rather wrong if I am reading it correctly. The fundamental difference between compound literal and cast operator is that the former is a lvalue, as by N1570 6.5.2.5/p4 (emphasis mine):
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.
while the latter is not, 6.5.4/p5 Cast operators (emphasis mine):
Preceding an expression by a parenthesized type name converts the
value of the expression to the named type. This construction is called
a cast.104)
104) A cast does not yield an lvalue. Thus, a cast to a qualified type
has the same effect as a cast to theunqualified version of the type.

What does "int *a = (int[2]){0, 2};" exactly do?

I was very surprised when I saw this notation. What does it do and what kind of C notion is it?
This is a compound literal as defined in section 6.5.2.5 of the C99 standard.
It's not part of the C++ language, so it's not surprising that C++ compilers don't compile it. (or Java or Ada compilers for that matter)
The value of the compound literal is that of an unnamed object initialized by the
initializer list. If the compound literal occurs outside the body of a function, the object
has static storage duration; otherwise, it has automatic storage duration associated with
the enclosing block.
So no, it won't destroy the stack. The compiler allocates storage for the object.
Parenthesis are put around the type and it is then followed by an initializer list - it's not a cast, as a bare initialiser list has no meaning in C99 syntax; instead, it is a postfix operator applied to a type which yields an object of the given type. You are not creating { 0, 3 } and casting it to an array, you're initialising an int[2] with the values 0 and 3.
As to why it's used, I can't see a good reason for it in your single line, although it might be that a could be reassigned to point at some other array, and so it's a shorter way of doing the first two lines of:
int default_a[] = { 0, 2 };
int *a = default_a;
if (some_test) a = get_another_array();
I've found it useful for passing temporary unions to functions
// fills an array of unions with a value
kin_array_fill ( array, ( kin_variant_t ) { .ref = value } )
This is a c99 construct, called a compound literal.
From the May 2005 committee draft section 6.5.2.5:
A postfix expression that consists of
a parenthesized type name followed by
a brace- enclosed list of initializers
is a compound literal. It provides an
unnamed object whose value is given by
the initializer list.
...
EXAMPLE 1 The file scope definition
int *p = (int []){2, 4};
initializes p
to point to the first element of an
array of two ints, the first having
the value two and the second, four.
The expressions in this compound
literal are required to be constant.
The unnamed object has static storage
duration.
Allocates, on the stack, space for [an array of] two ints.
Populates [the array of] the two ints with the values 0 and 2, respectively.
Declares a local variable of type int* and assigns to that variable the address of [the array of] the two ints.
(int[2]) tells the compiler that the following expression should be casted to int[2]. This is required since {0, 2} can be casted to different types, like long[2]. Cast occurs at compile time - not runtime.
The entire expression creates an array in memory and sets a to point to this array.
{0, 2} is the notation for an array consisting of 0 and 2.
(int[2]) casts it to an array (don't know why).
int * a = assigns it to the int pointer a.

Resources