compound-literals

Possible to initialize/assign a struct pointer?

自作多情 提交于 2021-01-28 11:53:50
问题 Let's say I have the following struct and two versions to initialize it: #include <stdio.h> typedef struct Car *CarPtr; typedef struct Car { const char* name; unsigned int price; } Car; int main(void) { Car ford = { .name = "Ford F-150", .price = 25000 }; print_struct(&ford); // is this possible to do in a single assignment? CarPtr jeep = { .name = "Jeep", .price = 40000 }; print_struct(jeep); } Is the second version possible to do directly? Or do I need to do something along the lines of:

Initialize static variable with element of const compound literal

≡放荡痞女 提交于 2020-01-06 08:48:17
问题 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 first comment doesn't make sense, because I'm asking explicitly about const literals, and not variables. 回答1: Yes, an element of a compound literal may be used as an initializer. C 2018 6.7.9 4 tells us what initializers must be: All the expressions in an

Define new function, array, struct etc inside of parameter of function call [duplicate]

纵饮孤独 提交于 2019-12-20 02:15:56
问题 This question already has answers here : How to pass a constant array literal to a function that takes a pointer without using a variable C/C++? (9 answers) Closed 3 years ago . 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 String[] { "hello", "my", "friend" }); Currently, I do the following in

Is it possible to pass a structure variable as a function argument without previously defining it?

一曲冷凌霜 提交于 2019-12-19 19:40:56
问题 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 have the following in main.c which works: hsv hsvCol = {i/255.0, 1, 1}; rgb col = hsv2rgb(hsvCol); I want to be able to just create the variable hsvCol inside the parameters for hsv2rgb without having to create the variable and passing it as a parameter. I've tried the each of the following (in place of

Array as compound literal [duplicate]

五迷三道 提交于 2019-12-19 03:36:33
问题 This question already has answers here : Why are compound literals in C modifiable (2 answers) Closed last year . 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 element of that unnamed array is modified with 100. So it seems as array as compound literal are lvalue and not constant value. 回答1: It

Why can't I pass constant arrays as arguments?

我是研究僧i 提交于 2019-12-17 16:44:39
问题 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 there a way that I could achieve something in C like this - generating and immediately passing an array known at compile time - that avoids having to spend a line of code pre-declaring and filling it? 回答1: Short answer: You need to make use of a compound literal. Something like arrayfn( (double[]) {1.0, 2

Why are compound literals in C modifiable

不想你离开。 提交于 2019-12-17 10:06:50
问题 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 completely modifiable (and looking at the generated machine code, you see they are pushed on the stack): char* str = (char[]){"Hello World"}; *str = 'B'; // A-Okay! I'm compiling with clang-703.0.29 . Shouldn't those two examples generate the exact same machine code? Is a compound literal really a literal, if

Lifetime of referenced compound array literals

房东的猫 提交于 2019-12-10 17:34:55
问题 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 some socket interface function where I don't care about the return name length, like this: int sockfamily(int fd) { struct sockaddr_storage ss; getpeername(fd, (struct sockaddr *)&ss, (socklen_t [1]){sizeof(ss)}); return(ss.ss_family); } Clearly,

Is there any way for a compound literal to have variable length in c99?

霸气de小男生 提交于 2019-12-10 16:24:20
问题 I know that arrays with lengths determined at runtime are possible by declaring the array normally: char buf[len]; and I know that I can declare an array as a compound litral and assign it to a pointer midway: char *buf; .... buf = (char[5]) {0}; However, combining the two doesn't work (is not allowed by the standard). My question is: Is there any way to achieve the effect of of the following code? (note len ) char *buf; .... buf = (char[len]) {0}; Thank you. 回答1: The language explicitly

Cryptic struct definition in C

时光总嘲笑我的痴心妄想 提交于 2019-12-10 02:06:14
问题 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 = &(maze_t) { .left = &(maze_t) { .left = &(maze_t) {}, .right = &(maze_t) {} }, .right = &(maze_t) { .right = &(maze_t) {} } }, .right = &(maze_t) { .tag = 8, .left = &(maze_t) {}, .right = &(maze_t) { .tag = 10, .left = &(maze_t) { .tag = 11, .left = &(maze_t) { .hasCheese = 1, .tag = 12 } }, .right = &