compound-literals Questions

1

Solved

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) { pr...
Antibiotic asked 27/3, 2021 at 12:48

5

Solved

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 ...
Pinochle asked 10/3, 2021 at 19:50

2

Solved

I am trying to understand why use this casting style in ProcessHacker Code. RtlSetHeapInformation( PhHeapHandle, HeapCompatibilityInformation, &(ULONG){ HEAP_COMPATIBILITY_LFH }, // HEAP_CO...
Bedesman asked 6/3, 2021 at 9:27

4

I was reading about compound literals, and I saw that they are l-values. So I suspected that you can assign them, so I did an experiment and I noticed that this compiles without warnings with gcc -...
Taillight asked 8/10, 2020 at 10:29

2

Solved

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 i...
Edict asked 7/7, 2020 at 13:18

2

Solved

I have a struct in C, which members are float arrays. I want to initialize it during compile time like this: typedef struct curve { float *xs; float *ys; int n; } curve; curve mycurve1 = ...
Terminable asked 12/2, 2020 at 19:58

2

Solved

Compiling with gcc -std=c99 -Wextra this piece of code: #include <stdio.h> struct T { int a; int *b; int c; }; int main(void) { struct T t = {.b = ((int []){1, 1})}; printf("%d\n", t...

2

Solved

One does usually associate 'unmodifiable' with the term literal char* str = "Hello World!"; *str = 'B'; // Bus Error! However when using compound literals, I quickly discovered they are complete...
Neighboring asked 17/4, 2016 at 11:55

1

Solved

In this code, a structure is defined as follows: typedef struct { int line; int column; } Pos; And later used this way: Pos get_pos ( int delta ) { ... return ( Pos ){ f->line, f->co...
Leotie asked 8/2, 2019 at 4:18

4

Solved

In GCC, I'm able to do this: (CachedPath){ino} inode->data = (struct Data)DATA_INIT; where: struct CachedPath { Ino ino; }; typedef int8_t Depth; struct Data { Offset size; Blkno root; ...
Touraco asked 6/10, 2010 at 6:24

3

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 Approa...
Nolannolana asked 23/8, 2018 at 22:41

3

Solved

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: voi...
Exactitude asked 12/2, 2018 at 20:3

2

Solved

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...
Fireguard asked 7/12, 2017 at 9:36

2

Solved

From what I understood, I am passing the address of the variable a to the function int ffx1. After that, what exactly does this line p = (int[2]){*p}; mean? int ffx1(int * p) { p = (int[2]){*p...
Sylvan asked 31/10, 2017 at 17:36

2

Solved

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 th...
Whited asked 17/4, 2017 at 18:56

1

Solved

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 ...
Mendelevium asked 20/2, 2017 at 16:55

1

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 ARRA...
Matador asked 29/11, 2016 at 16:58

1

Solved

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 p...
Roentgenology asked 19/9, 2016 at 9:58

1

Solved

In C, why can't I do this: arrayfn({1.0, 2.0, 3.0}); if arrayfn is some function that takes in one parameter of type double[] or double*, whichever. Trying this gives me a syntax error. Is ther...
Gert asked 22/4, 2016 at 15:14

1

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 pro...
Sendal asked 15/12, 2015 at 14:42

5

Solved

I have encountered a strange behaviour when using compound literals for static struct initialization in GCC in c99/gnu99 modes. Apparently this is fine: struct Test { int a; }; static struct ...
Penrose asked 6/8, 2015 at 18:42

2

I saw this here and I don't know what it means: &(int) { 1 } I thought it was weird because it seems like invalid syntax. It's casting a block scope(?) with a random 1 in the middle (w...
Riti asked 28/7, 2015 at 2:4

2

Solved

This compiles without warnings using clang. typedef struct { int option; int value; } someType; someType *init(someType *ptr) { *ptr = (someType) { .option = ptr->option | ANOTHEROPT, .va...
Antipodes asked 19/2, 2014 at 13:53

2

Solved

Compound Literals are a C99 construct. Even though I can do this in C++ : #include <iostream> using namespace std; int main() { for (auto i : (float[2]) {2.7, 3.1}) cout << i <&lt...
Bodkin asked 23/1, 2015 at 18:33

2

Solved

I came across the following maze definition code: typedef struct mazeNode { int hasCheese; int tag; struct mazeNode *left; struct mazeNode *right; } maze_t; maze_t maze = { .tag = 1, .left ...
Tema asked 30/8, 2014 at 1:17

© 2022 - 2024 — McMap. All rights reserved.