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

Initialize static variable with element of const compound literal

Is a const compound literal a valid initializer for a static variable? #define COMPOUND ((const int [2]){1, 2}) static const int x = COMPOUND[0]; /* static const int x = 1; should be equivalent */ EDIT: The possible duplicacte in the…
0
votes
1 answer

How to pass an array of pointer to structures to a function?

Consider a structure that represents a point in cartesian co-ordinate. struct point { float x, y; }; typedef struct point point_t; I have a function that takes in a bunch of points and draws a curve based on the points passed, whose definition…
sigsegv
  • 159
  • 7
0
votes
1 answer

Initialize Variable Inside Compound Literal

I'm fairly new to C and only just stumbled onto Compound Literals so please correct me if my question is inaccurate. I have a struct; typedef struct { int someVal; } foo; Now I understand this could be initialized with the following. int…
0
votes
4 answers

Function for allocating arrays in other function in C

I am having problems when using a function to allocate arrays in another function. Here is the segment that causes problems: void array_allocator(int method, int** a, int** b){ if (method == 0) { (*a) = (int[5]) {0, 1, 2, 3, 4}; (*b) =…
0
votes
2 answers

C99 pointer to compound literal array of pointers

NOTE: I am actively fiddling with this over on Ideone. I have a (self-referential) structure: typedef struct T_Function T_Function; struct T_Function { T_Function * (* inhibits)[]; // pointer to array of pointers to this structure }; and would…
altendky
  • 4,176
  • 4
  • 29
  • 39
0
votes
4 answers

#define a constant struct

Let's say I have a struct: struct location { int x; int y; }; Then I want to define a invalid location for use later in the program: #define INVALID_LOCATION (struct location){INT_MAX,INT_MAX} However when I use that in my program, it…
sigvardsen
  • 1,531
  • 3
  • 26
  • 44
0
votes
1 answer

Why do I need a compound literal like temporary construction to initialise my std::array member?

Consider this minimal example: #include struct X { std::array a; X(int i, int j) : a(std::array{{i,j}}) {} // ^^^^^^^^^^^^^^^^^^ ^ }; According to other posts I shouldn't have to explicitly…
bitmask
  • 32,434
  • 14
  • 99
  • 159
0
votes
4 answers

Compound literals in IF statement

I tried this small code to use compound literals in IF statement: #include struct time { int hour; int minutes; int seconds; }; int main(void) { struct time testTimes; testTimes = (struct time){12,23,34}; if…
KawaiKx
  • 9,558
  • 19
  • 72
  • 111
-1
votes
1 answer

Cannot assign address to pointer array?

I am developing a homekit device using ESP8266 and I am unable to programmatically generate services. I need to generate 16 of them. Here is the code I attempted to assign to *service: homekit_service_t *services[18] = { &(homekit_service_t){ …
BlackLotus
  • 496
  • 2
  • 9
  • 24
-1
votes
2 answers

How to change array member of a structure in C

I am trying to program a microcontroller to be able to communicate with external flash memory chip which utilizes SPI. The operation code (opcode) followed by address bytes then data bytes has to be sent in order. Instead of defining these bytes…
John Lenon
  • 13
  • 2
-1
votes
2 answers

Compound literal pointer passed as argument in C

Is passing a cast pointer by value valid in C? If not why is this working? #include typedef struct { int size; char* str; } MY_STRUCT; void print_my_struct_prt(MY_STRUCT* mstrct) { printf("%s%d%s%s\n", "size: ",…
funcs
  • 43
  • 6
-1
votes
1 answer

C - use a 2d-array as argument

I'd written a tiny c program. The code compiled successfully by GCC 4.8.5 20150623 (Red Hat 4.8.5-4) with -std=c99, but gave me a warning. I cannot deal with the warning, and I even don't known what's wrong with it. The code is here: #include…
1 2 3 4 5 6 7
8