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
34
votes
2 answers

Are compound literals Standard C++?

Compound Literals are a C99 construct. Even though I can do this in C++ : #include using namespace std; int main() { for (auto i : (float[2]) {2.7, 3.1}) cout << i << endl; } It seems that for example MSVC supports it as an…
Lorah Attkins
  • 5,331
  • 3
  • 29
  • 63
33
votes
5 answers

C compound literals, pointer to arrays

I'm trying to assign a compound literal to a variable, but it seems not to work, see: int *p[] = (int *[]) {{1,2,3},{4,5,6}}; I got a error in gcc. but if I write only this: int p[] = (int []) {1,2,3,4,5,6}; Then it's okay. But is not what I…
drigoSkalWalker
  • 2,742
  • 9
  • 32
  • 45
19
votes
3 answers

Why use functions like CGRectMake?

I'm curious why functions like CGRectMake and CGPointMake exist and are widely used. when, instead, you can do: (CGRect){{x, y}, {width, height}} surely this is more efficient (though I'm guessing not by much) as there is no function call? Also you…
Jonathan.
  • 53,997
  • 54
  • 186
  • 290
18
votes
2 answers

Why are compound literals in C modifiable

One does usually associate 'unmodifiable' with the term literal char* str = "Hello World!"; *str = 'B'; // Bus Error! However when using compound literals, I quickly discovered they are completely modifiable (and looking at the generated machine…
hgiesel
  • 5,430
  • 2
  • 29
  • 56
17
votes
2 answers

Compound literal and designated initializer warning from GCC but not Clang

Compiling with gcc -std=c99 -Wextra this piece of code: #include struct T { int a; int *b; int c; }; int main(void) { struct T t = {.b = ((int []){1, 1})}; printf("%d\n", t.b[1]); return 0; } Is giving me a…
David Ranieri
  • 39,972
  • 7
  • 52
  • 94
17
votes
5 answers

static struct initialization in c99

I have encountered a strange behaviour when using compound literals for static struct initialization in GCC in c99/gnu99 modes. Apparently this is fine: struct Test { int a; }; static struct Test tt = {1}; /* 1 */ However, this is not: static…
Ganil
  • 519
  • 5
  • 13
13
votes
1 answer

C - Creating an anonymous struct instance

In this code, a structure is defined as follows: typedef struct { int line; int column; } Pos; And later used this way: Pos get_pos ( int delta ) { ... return ( Pos ){ f->line, f->column + delta }; } The line, return ( Pos ){…
Jet Blue
  • 5,109
  • 7
  • 36
  • 48
10
votes
1 answer

Why can't I pass constant arrays as arguments?

In C, why can't I do this: arrayfn({1.0, 2.0, 3.0}); if arrayfn is some function that takes in one parameter of type double[] or double*, whichever. Trying this gives me a syntax error. Is there a way that I could achieve something in C like this -…
Billy Smith
  • 273
  • 1
  • 5
10
votes
2 answers

Cryptic struct definition in C

I came across the following maze definition code: typedef struct mazeNode { int hasCheese; int tag; struct mazeNode *left; struct mazeNode *right; } maze_t; maze_t maze = { .tag = 1, .left = &(maze_t) { .left =…
liv2hak
  • 14,472
  • 53
  • 157
  • 270
10
votes
2 answers

What is the lifetime of compound literals passed as arguments?

This compiles without warnings using clang. typedef struct { int option; int value; } someType; someType *init(someType *ptr) { *ptr = (someType) { .option = ptr->option | ANOTHEROPT, .value = 1 }; return ptr; } int main() { …
vestol
  • 167
  • 7
9
votes
4 answers

How is assignments to compound literals useful?

I was reading about compound literals, and I saw that they are l-values. So I suspected that you can assign them, so I did an experiment and I noticed that this compiles without warnings with gcc -Wall -Wextra -pedantic: struct foo { int x; …
klutt
  • 30,332
  • 17
  • 55
  • 95
9
votes
3 answers

Compound literals and function-like macros: bug in gcc or the C standard?

In C99, we have compound literals, and they can be passed to functions as in: f((int[2]){ 1, 2 }); However, if f is not a function but rather a function-like macro, gcc barfs on this due to the preprocessor parsing it not as one argument but as two…
R.. GitHub STOP HELPING ICE
  • 208,859
  • 35
  • 376
  • 711
9
votes
2 answers

What is the meaning of "p = (int[2]){*p};" line in C pointer?

From what I understood, I am passing the address of the variable a to the function int ffx1. After that, what exactly does this line p = (int[2]){*p}; mean? int ffx1(int * p) { p = (int[2]){*p}; return(p[1]); } int main() { int a =…
hago
  • 315
  • 2
  • 9
9
votes
4 answers

Where does the compound/string literals get stored in the memory?

I read that ; A compound literal is a C99 feature that can be used to create an array with no name. Consider the example: int *p = (int []){3, 0, 3, 4, 1}; p points to the first element of a five- element array containing 3, 0, 3, 4, and…
haccks
  • 104,019
  • 25
  • 176
  • 264
8
votes
2 answers

Initializing struct containing arrays

I have a struct in C, which members are float arrays. I want to initialize it during compile time like this: typedef struct curve { float *xs; …
JonnyRobbie
  • 526
  • 5
  • 16
1
2 3 4 5 6 7 8