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

clang and gcc different behavior with compound literal

Came across compound literal recently, and as far as I understand it, the following is the correct way of using it. Fortunately, it works both with gcc and clang on ubuntu. int main() { int *p = (int []) {1, 2}; return 0; } However, I notice…
Albert Netymk
  • 1,102
  • 8
  • 24
2
votes
2 answers

Is it safe to return a pointer to a compound literal declared within a function or should you use malloc?

Is it safe to return the pointer to a compound literal from a function? I know the local variables of a function get deallocated after the function returns and that malloc'd memory is an exception to that. However, the compiler isn't giving any…
2
votes
3 answers

Why is my initializer constant in one version, but not valid another version?

I'm trying to declare a static array of structures and can do so when it is declared globally, but not when declared static within a function. Here's some sample code that works properly: #include enum { Mammals, Amphibians, …
abelenky
  • 63,815
  • 23
  • 109
  • 159
2
votes
1 answer

warning: initialization of 'int *' from ' int' makes pointer from integer without a cast, when assigning array to a int pointer

I was studying pointers and this is what i learned:- int a = 23; int *ptr = &a; char b = 'b'; char *pnt = &b; char *str = "string"; Value assigned to a pointer is an address. So i can't do int *ptr = 7; or char *k = 'c';. But i can do char *str =…
tycoon
  • 121
  • 9
2
votes
2 answers

Compound literals in C: do they create duplicate copies?

I have always wondered about compound literals in C, do they create duplicate copies? Take the following two examples for instance. The only difference between the two are few lines of code, respectively book->book_id = book_id; book->price =…
madmurphy
  • 1,451
  • 11
  • 20
2
votes
1 answer

Difference between two struct initializations

What is the difference between the following two initializations for a Struct? Car ford = { .name = "Ford F-150", .price = 25000 }; And: Car dodge = (Car) { .name = "Ram", .price = 1000 }; From Compiler Explorer, it looks like the…
David542
  • 104,438
  • 178
  • 489
  • 842
2
votes
1 answer

Return compound literal

Look at this code. I return an address of the compound literal here. #include #define FOO(bar) ((bar)->a + (bar)->b) struct bar { int a; int b; }; static struct bar * to_bar(int a, int b); int main(void) { int baz =…
2
votes
1 answer

What curly braces inside a function call mean in C?

I tried to solve exercise 1-24 in K&R C book in which you have to create a program which can detect basic syntax errors (unbalanced parentheses, brackets and so on). I ran some tests to debug it on C source files scattered on my system. My program…
Vilat
  • 23
  • 2
2
votes
0 answers

gcc and clang don't emit a warning when returning a pointer to a compound literal

Based on the explanation of https://en.cppreference.com/w/c/language/compound_literal: The unnamed object to which the compound literal evaluates has static storage duration if the compound literal occurs at file scope and automatic storage…
David Ranieri
  • 39,972
  • 7
  • 52
  • 94
2
votes
1 answer

Initialize pointer to a struct with a compound literal

I'm new to C and I'm trying to understand the syntax for compound literals. My question is similar to Initializing a pointer to compound literals in C but I don't think that answers it. If have a struct and a type defined as a pointer to the struct…
avy
  • 644
  • 6
  • 16
2
votes
1 answer

Difference between string literal and const char[], or char[]

Let's say I have this C code f((char []){ "hello" }); f((const char []){ "hello" }); f("hello"); In all three cases, hello is copied into the function. a pointer to the first character of the char array is initialized as the function parameter. I…
hgiesel
  • 5,430
  • 2
  • 29
  • 56
2
votes
1 answer

Why are compound literals not part of C++ so far?

I know that C & C++ are different languages standardized by different committees. I know that like C efficiency has been a major design goal for C++ from the beginning. So, I think if any feature doesn't incur any runtime overhead & if it is…
Destructor
  • 14,123
  • 11
  • 61
  • 126
2
votes
1 answer

How to iterate over the compound literal array

​How can I iterate over the compound literal array so that I can print book_id and value? #include #include typedef struct { int book_id; char value; } BookCode; typedef struct { BookCode *codes; } Books; int…
user1024718
  • 573
  • 6
  • 18
2
votes
3 answers

storage duration of compound literals

I somehow can't comprehend how the storage duration of compound literals defined in blocks is automatic, and the reasoning is as follows: let us assume that the compound literal is defined in a function or block that is called repeatedly; when this…
Lockon2000
  • 343
  • 1
  • 3
  • 14
2
votes
1 answer

Assigning a compound literal to a struct with const fields

I have the following code: test.c struct test { int const i; }; void init_test(struct test *t) { *t = (struct test){42}; } int main(int argc, char **argv) { (void)argc; (void)argv; struct test t; init_test(&t); /* I would…
Andrew
  • 23
  • 1
  • 2