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
5
votes
1 answer

Need explanation of weird syntax

i follow tutorial and find this code: self.imageView.frame = (CGRect){.origin = CGPointMake(0.0f, 0.0f), .size = image.size}; Its pretty clear What it does, but I'm not understand syntax of this line of code. First time i see something like this:…
Evgeniy Kleban
  • 6,794
  • 13
  • 54
  • 107
5
votes
1 answer

Lifetime of referenced compound array literals

I only recently learned that I can actually use references to compound literal arrays in C, which I find useful, but I don't quite understand how it works. For instance, say that I use the feature to avoid having to declare a variable for a call…
Dolda2000
  • 25,216
  • 4
  • 51
  • 92
4
votes
1 answer

Compound literal is created once for a given scope

I'm pretty confused about N2346::6.5.2.5/15 and N2346::6.5.2.5/16 which states (emp. mine) 15 EXAMPLE 8 Each compound literal creates only a single object in a given scope struct s { int i; }; int f (void) { struct s *p = 0, *q; int j = 0; …
Some Name
  • 8,555
  • 5
  • 27
  • 77
4
votes
2 answers

What are the advantages of using "{}" for casting in C Language?

I am trying to understand why use this casting style in ProcessHacker Code. RtlSetHeapInformation( PhHeapHandle, HeapCompatibilityInformation, &(ULONG){ HEAP_COMPATIBILITY_LFH }, // HEAP_COMPATIBILITY_LFH = 2UL …
Adam
  • 51
  • 5
4
votes
1 answer

Compound literals for scalar types

Since c99 compound literals can be used to e.g. initialize pointers: int *p = (int []) {1, 2, 3, 4}; while this is usually used for structs it can be used to initialize anonymous arrays as well (see example). But if I understood this correctly…
lord.garbage
  • 5,884
  • 5
  • 36
  • 55
4
votes
1 answer

Why doesn't compound literals assignment work without a typecast

I have a question about literals in C. int a; //a is an integer that is assigned an integer literal 414 a = 414; float b; //b is a float that is assigned a float literal of 3.14 b = 3.14; struct point { int x,y; }; struct point b; //{5,6} is…
liv2hak
  • 14,472
  • 53
  • 157
  • 270
4
votes
4 answers

Array as compound literal

In C99 we can use compound literals as unnamed array. But are this literals constants like for example 100, 'c', 123.4f, etc. I noticed that I can do: ((int []) {1,2,3})[0] = 100; and, I have no compilation error and is guessable that the first…
xdevel2000
  • 20,780
  • 41
  • 129
  • 196
3
votes
2 answers

Initialize block-scope static const variable with pointer to compound literal?

The following code is rejected by GCC and Clang (godbolt link): struct thing; typedef enum { THING_TYPE_A, THING_TYPE_B, } thing_type_t; typedef struct thing_a { int i; } thing_a_t; typedef struct thing_b { struct thing const *t; }…
Charles Nicholson
  • 888
  • 1
  • 8
  • 21
3
votes
1 answer

Why does Clang complain an "initializer element is not a compile-time constant" for a local compound literal?

With the following code, #define REINT(T, X) (union {__typeof__(X) x; T t;}){X}.t int f2i(float x) { return REINT(int, x); } float i2f(int x) { return REINT(float, x); } float f2i2f(float x) { return REINT(float, REINT(int, x)); } Clang…
xiver77
  • 2,162
  • 1
  • 2
  • 12
3
votes
2 answers

Why can't a defined, fix-sized array be assigned using a compound literal?

Why is it so that a struct can be assigned after defining it using a compound literal (case b) in sample code), while an array cannot (case c))? I understand that case a) does not work as at that point compiler has no clue of the memory layout on…
davidanderle
  • 638
  • 6
  • 12
3
votes
1 answer

How to declare and pass a structure during function invocation?

It is very common to declare and pass a basic data-type variable during a function invocation, can we achieve something similar with the structures ? Below code explains my question better. struct s { int i; char c; }; void f(int i) { …
m0hithreddy
  • 1,752
  • 1
  • 10
  • 17
3
votes
1 answer

How to properly pass an array as a function argument?

When I try to send an array in to the function I get an error. This is my minunit test program: #include "minunit.h" #include "calc.h" #include int tests_run = 0; static char * test_Repetitve() { mu_assert("error in…
3
votes
2 answers

Reflecting inner struct in outer struct initialization

I have a code of structs stated as below: typedef struct A { B numberOfB[3]; } A; typedef struct B { int number1; int number2; boolean bool1; boolean bool2; } B; In the source code I have an initialization which looks like this: A*…
Lisek
  • 753
  • 2
  • 11
  • 31
3
votes
1 answer

Given a pointer to a structure, can I assign the structure the result of an aggregate-initializer in one line?

For example, given a structure S: typedef struct { int a, b; } S; ... and a method which takes a pointer to S, can I assign it the value of an aggregate initializer1 all in one line? Here's my existing solution which uses a temporary: void…
BeeOnRope
  • 60,350
  • 16
  • 207
  • 386
3
votes
3 answers

Define new function, array, struct etc inside of parameter of function call

If you had a function which took the following: void foo(char **arr); How can you do the following: void foo(char* x[] = { "hello", "my", "friend" }); If this confuses you, in Java we do this by the following: public void foo(String[] x); foo(new…
Hatefiend
  • 3,416
  • 6
  • 33
  • 74