Questions tagged [compound-literals]

Use this tag to ask questions about the semantics or usage of compound literal, which was introduced in C99.

Semantics

  • 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.

  • 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.

  • 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.

117 questions
1
vote
1 answer

Is a new object created each time a compound literal is assigned to a pointer in a loop?

According to C99 standard 6.5.2.5 .9 the code: 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…
Dariusz
  • 21,561
  • 9
  • 74
  • 114
1
vote
2 answers

How does compound literals work in this code?

I have the following code in which I wrote two functions. Both are meant to produce the same output. But the function g() which has loop produces a different output from what I had expected as shown below. #include struct S { int i;…
cpx
  • 17,009
  • 20
  • 87
  • 142
0
votes
5 answers

assigning a compound literal to an array pointer gives both the expected result and rubbish at the same place and time?

#include int main(void) { int a[5], *p, i; p = a; p = (int []){1, 2, 3, 4, 5}; for (i = 0; i < 5; i++, p++) { printf("%d == %d\n", *p, a[i]); } return 0; } Lo and behold (YMMV): $ gcc -O -Wall -Wextra…
anon
0
votes
1 answer

What triggers the computer to release a Compound literal?

The problem is in the function insert_envar: char **get_envaddress(char **envp, char *find) { int i; int length; length = ft_strlen(find); i = 0; while (envp[i]) { if (!ft_strncmp(find, envp[i], length)) …
0
votes
2 answers

Flat-list initializer for an array in C

An exemplary 2D array initialization goes like this: int arr[3][5] = { {0,1,2,3,4}, {5,6,7,8,9}, {10,11,12,13,14} }; We are taught that this is the "correct way" of initializing an array. But is it really "correct" as if it is incorrect to use a…
Edenia
  • 2,312
  • 1
  • 16
  • 33
0
votes
3 answers

Initializing a dynamically allocated array with a compound literal

I am allocating memory for my float3x3 matrix as such: typedef float float3x3[3][3]; float3x3 *g = malloc(sizeof g); g = &(float3x3){ { 1, 0, 0 }, { 0, 1, 0 }, { 0, 0, 1 } }; printfloat3x3(*g); The above compiles, however I am…
0
votes
1 answer

Array type safety function arguments accessed with compound literal

There is a method which is defined in this manner: void corruption(int (*idata[1]), bool (*flags[3]), bool* (*finfo[2]), int* (*error[1])); I am trying to pass information to it using compound literals while also respecting type-safety definition,…
Amaterastis
  • 477
  • 3
  • 12
0
votes
1 answer

Double evaluation within macro: a case of sizeof() to determine array's size passed as compound literal

C99 makes it possible to define arrays basically anywhere, as compound literals. For example, given a trivial function sumf() that accepts an array of float as input, we would expect the prototype to be : float sumf(const float* arrayf, size_t…
Cyan
  • 13,248
  • 8
  • 43
  • 78
0
votes
2 answers

Zero'ing an array before adding an entry

Does the following zero out a list and then add the first entry? Or does it just add the first entry to handlers? Logger = (struct logger) {.level=INFO, .format=DEFAULT_FORMAT, .num_handlers=1, .handlers[0]=stdout}; For example, does this…
carl.hiass
  • 1,526
  • 1
  • 6
  • 26
0
votes
2 answers

Why the value is changed for the Read-Only Compound Literal?

I was studying the Read-Only compound literals in C. And when I tried to change its value with the help of dereference operator, the value got changed!! I am now confused why is that so. Also when I compiled and run the program (Without attempting…
0
votes
2 answers

A portable C++ alternative to compound literals that is guaranteed to be free of heap allocation

C99 compound literals are not supported in C++. In many cases, list intialization provides an excellent alternative. However, they are not guaranteed not to heap-allocate memory. Are there any convenient and portable alternatives for C++ code that…
gspr
  • 11,144
  • 3
  • 41
  • 74
0
votes
2 answers

C error: taking address of temporary array

I am passing a two-dimensional char array as a compound literal into a method but am receiving the error: error: taking address of temporary array Does this have anything to do with the way the variable is being declared in the method declaration…
0
votes
1 answer

How to assign value to multiple member a structure at once

Let's assume that we have a struct that has 4x 1-byte members. I want to use Xyz as a memory address and cast it as a 32bit pointer then I will assign values to it. By this, I would able to set all the byte members at once. This is just an example…
0
votes
2 answers

Array Index Types & Warning (752) in XC8 v2.10 C99

First question, so I hope its understandable! I am initialising a pointer to a compound literal, which is an array of struct pointers. I can then use this pointer as I would normally an array of pointers. typedef struct { uint32_t test1; …
jfcroft245
  • 13
  • 3
0
votes
2 answers

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…
St.Antario
  • 26,175
  • 41
  • 130
  • 318