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
8
votes
3 answers

Struct vs string literals? Read only vs read-write?

Does the C99 standard permit writing to compound literals (structs)? It seems it doesn't provide writing to literal strings. I ask about this because it says in C Programming: A Modern Approach, 2nd Edition on Page 406. Q. Allowing a pointer to a…
Evan Carroll
  • 78,363
  • 46
  • 261
  • 468
8
votes
1 answer

Is it possible to pass a structure variable as a function argument without previously defining it?

I have two structs defined as so (in color.h): typedef struct rgb { uint8_t r, g, b; } rgb; typedef struct hsv { float h, s, v; } hsv; hsv rgb2hsv(rgb color); rgb hsv2rgb(hsv color); I then have the following in main.c which works: hsv hsvCol…
Jamie Scott
  • 452
  • 4
  • 20
8
votes
1 answer

sizeof compound literal array

I'm trying to statically allocate some structures, each containing two members: a pointer to an array of structures, and the size of that array. Here's a working version of the code: #define ARRAY_SIZE(x) (sizeof((x)) / sizeof((x)[0])) struct…
Chase Patterson
  • 169
  • 1
  • 8
8
votes
1 answer

Passing values directly as array to function in C

I have a function which accepts an integer array as argument and print it. void printArray(int arr[3]) { int i; for(i=0; i<3; ++i) { printf("\n%d", arr[i]); } } Is there a way to pass the values of the array like this printArray(…
J...S
  • 5,079
  • 1
  • 20
  • 35
8
votes
1 answer

Is it possible (legal) to assign an anonymous union in a compound literal?

I have a struct: typedef struct _n { int type; union { char *s; int i; }; } n; When I try to assign a compound literal, like: node n1 = {1, 0}; node n2 = {2, "test"}; gcc gives me some warnings such as: warning:…
Steve
  • 1,849
  • 2
  • 19
  • 19
7
votes
1 answer

How can I pass a struct compound literal to a function as argument?

Here is a struct that I have: typedef struct { float r, g, b; } color_t; I'd like to pass a compound literal of this struct to a function as argument, like this: void printcolor(color_t c) { printf("color is : %f %f %f\n", c.r, c.g,…
user14949034
7
votes
1 answer

How to use compound literals to `fprintf()` multiple formatted numbers with arbitrary bases?

I'd like to convert multiple numbers into some representation and then use the flags, width and precision of *printf() specifiers. Preference would be to avoid global or static buffers. The key problem appears to be is how to provide a char[] for…
chux - Reinstate Monica
  • 143,097
  • 13
  • 135
  • 256
6
votes
5 answers

Why is an explicit cast necessary in the following struct definition

With struct initialization via a compound literal, it will do the casting itself. For example: struct movie { char title[50]; int year; }; typedef struct movie Item; typedef struct node { Item item; struct node *next; }…
David542
  • 104,438
  • 178
  • 489
  • 842
6
votes
2 answers

Lifetime of a compound literal

6.5.2.5p5 says 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. Am I correct to interpret "the enclosing…
Petr Skocik
  • 58,047
  • 6
  • 95
  • 142
6
votes
1 answer

Is there any way for a compound literal to have variable length in c99?

I know that arrays with lengths determined at runtime are possible by declaring the array normally: char buf[len]; and I know that I can declare an array as a compound litral and assign it to a pointer midway: char *buf; .... buf = (char[5])…
seininn
  • 411
  • 5
  • 13
5
votes
2 answers

Compund literals storage duration in C

First question ever :) I'm studying programming "by myself", reading "C Programming: A modern Approach" by K.N.King. In Chapter18 - Declarations, in the Q&A section, there is a question about why selection statements and iteration statements (and…
Masfeo
  • 53
  • 3
5
votes
3 answers

Using pointed to content in assignment of a pointer

It has always been my understanding that the lack of a sequence point after the reading of the right expression in an assignment makes an example like the following produce undefined behavior: void f(void) { int *p; /*...*/ p = (int…
Kyle
  • 878
  • 6
  • 14
5
votes
0 answers

Can I specify section attribute to compound literals?

With GCC, I can do this: typedef struct { char a; int n; } MyStruct; MyStruct ms __attribute__((section("MySection"))) = {'x', 33}; MyStruct *pms = &ms; But when I use compound literal as follows, GCC complains warning: 'section' attribute…
dingcurie
  • 121
  • 6
5
votes
2 answers

Why do I need to type cast when initializing a user-defined structure using pointer?

I have this structure definition: typedef struct node_bst { int data; struct node_bst *lchild; struct node_bst *rchild; struct node_bst *parent; } node_bst; I tried to create a pointer to the structure using this: node_bst *root; and…
Nityesh Agarwal
  • 464
  • 2
  • 5
  • 18
5
votes
1 answer

Nested structures/arrays initialization

I have a structure that contains an arrays of another structure, it looks something like this: typedef struct bla Bla; typedef struct point Point; struct point { int x, y; }; struct bla { int another_var; Point *foo; }; I now want to…
quinmars
  • 11,175
  • 8
  • 32
  • 41