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

Fix gcc warnings with _Generic macro and compound literals

I have a struct with a union and an enum of its kind in it. I've made a macro that outputs a compound literal of the struct, that sets the kind and the data of the union according to the type passed to the macro use _Generic. Example code: #include…
GGG
  • 295
  • 1
  • 9
1
vote
1 answer

Why would some one define a pointer like this?

const char** list_entry[] = { (const char*[]){"ent1", "ent2", "ent3", "ent4"}, (const char*[]){"ent1", "ent2", "ent3", "ent4"} }; I don't quite understand as I could not think of anything I couldn't accomplish with only one "*" instead of…
1
vote
3 answers

Is it valid to pass a compound literal into a function?

I have a struct type as a parameter and need to pass it into function. The entire code is below: void insert(struct node *newt) { struct node *node = head, *prev = NULL; while (node != NULL && node->data < newt->data) { prev = node; …
Steven
  • 811
  • 4
  • 23
1
vote
1 answer

How to use a member of a structure defined by a compound literal?

I´ve found this piece of code, which uses a pointer to a structure made by a compound literal: int main() { struct s {int i; int x;} *p; int j = 0; p = &((struct s){ j++ }); } My questions are: How is j++ a valid expression for…
1
vote
2 answers

compound literals and pointers

It's safe initialize pointers using compound literals in such way and it's possible at all?: #include #include void numbers(int **p) { *p = (int []){1, 2, 3}; } void chars(char **p) { *p =…
1
vote
1 answer

Initializing a pointer to compound literals in C

Here is one not-so-common way of initializing the pointer: int *p = (int[10]){[1]=1}; Here, pointer point to compound literals. #include int main(void) { int *p = (int[10]){[1]=1}; printf("%d\n", p[1]); } Output: 1 This program…
msc
  • 33,420
  • 29
  • 119
  • 214
1
vote
1 answer

Any way to initialize a dynamically allocated structure?

C11 gives us a neat struct initialization syntax: struct some_struct {int some; char value;}; struct some_struct s = {.some = 5, .value = 'a'}; printf("some = %d, value = %c\n", s.some, s.value); http://ideone.com/zZxTc4 However, it doesn’t seem to…
user4385532
1
vote
1 answer

C struct array element initialization casting

Using C99 I'm trying to do this within a function: foo_t foos[4]; foos[0] = {1, {1,2}}; doesn't work. So I tried this... foo_t foos[4]; foos[0] = (foo_t){1, {1,2}}; which works, but is it safe? Is there not perhaps a better way to do this?
Edwin Skeevers
  • 309
  • 1
  • 8
1
vote
2 answers

How can I properly assign a value to an array (of structure type) element?

#include struct virus { char signature[25]; int size; }v[2]; int main(void) { static v[0] = {"Yankee",1813}; static v[1] = {"Doodle",2813}; int i; for(i=0;i<=1;i++) { printf("%s…
user7425291
1
vote
1 answer

Pass array of structs as compound literal to a function

I have an array declaration where I have initialized all the elements of the array with some initial value. I want to now create the members of the array "on the fly"/on-demand without having to statically allocate all the members. test_t…
thegeeklife
  • 107
  • 6
1
vote
2 answers

Inline struct usage

Considering this discussion. I have the following code list << (Database::ParameterX){"id_raw_tb", 100000}; Where, QList list; and struct ParameterX { QString name; double max; }; But Qt Creator 3.3.0 suggest me it's…
nsejxT nsejxT
  • 87
  • 1
  • 4
1
vote
3 answers

strategy to declare complex C structured const data?

I have a complex data structure (with lots of incomplete array types / heterogenous length arrays of structures and pointer to structures to arrays of structs ...) I would like to put those in flash memory, so I thought about putting them in const…
makapuf
  • 1,370
  • 1
  • 13
  • 23
1
vote
1 answer

error: taking address of temporary [-fpermissive] while compiling &(int) {}

I discovered few days ago Compound literals in the answer How do I use setsockopt(SO_REUSEADDR)? So I tried to compile simple code : #include int main() { int * ptr = &(int) {3}; printf("%d\n", *ptr); return 0; } Using gcc…
mpromonet
  • 11,326
  • 43
  • 62
  • 91
1
vote
1 answer

Error: A compound literal of type not allowed

I have a structure containing pointers to callback functions like such: typedef void (*LOAD_CB)(resource* r); typedef void (*UNLOAD_CB)(resource* r); typedef void (*CREATE_CB)(void* i); typedef void (*DESTROY_CB)(void* i); typedef…
gbbofh
  • 13
  • 1
  • 5
1
vote
3 answers

How to determine how many elements there are in an array of compound literals in C?

How can I determine how many elements there are in an array of compound literals. I'm using the well known macro sizeof(a)/sizeof(a[0]); but keep getting 1. #include typedef struct { int enable; const char * const *message; }…
Pete Darrow
  • 455
  • 5
  • 20