compound-literals

Initializing a pointer to compound literals in C

醉酒当歌 提交于 2019-12-08 05:19:06
问题 Here is one not-so-common way of initializing the pointer: int *p = (int[10]){[1]=1}; Here, pointer point to compound literals. #include <stdio.h> int main(void) { int *p = (int[10]){[1]=1}; printf("%d\n", p[1]); } Output: 1 This program is compiled and run fine in G++ compiler. So, Is it the correct way to initializing a pointer to compound literals? or Is it undefined behaviour initialize pointer to compound literals? 回答1: Yes, it is valid to have a pointer to compound literals. Standard

Initializing a pointer to compound literals in C

喜欢而已 提交于 2019-12-06 16:18:53
Here is one not-so-common way of initializing the pointer: int *p = (int[10]){[1]=1}; Here, pointer point to compound literals. #include <stdio.h> int main(void) { int *p = (int[10]){[1]=1}; printf("%d\n", p[1]); } Output: 1 This program is compiled and run fine in G++ compiler. So, Is it the correct way to initializing a pointer to compound literals? or Is it undefined behaviour initialize pointer to compound literals? Yes, it is valid to have a pointer to compound literals. Standard allows this. n1570-§6.5.2.5 (p8): EXAMPLE 1 The file scope definition int *p = (int []){2, 4}; initializes p

Struct vs string literals? Read only vs read-write? [duplicate]

被刻印的时光 ゝ 提交于 2019-12-05 22:43:08
问题 This question already has answers here : Why are compound literals in C modifiable (2 answers) Why do I get a segmentation fault when writing to a string initialized with “char *s” but not “char s[]”? (17 answers) Closed last year . 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 Approach, 2nd Edition on Page 406. Q. Allowing a pointer to a compound literal

Cryptic struct definition in C

人盡茶涼 提交于 2019-12-05 01:20:59
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 = &(maze_t) {} } } }; From the linked blog post I understand that they are trying to define the binary tree

Why use functions like CGRectMake?

我的未来我决定 提交于 2019-12-04 15:56:37
问题 I'm curious why functions like CGRectMake and CGPointMake exist and are widely used. when, instead, you can do: (CGRect){{x, y}, {width, height}} surely this is more efficient (though I'm guessing not by much) as there is no function call? Also you can set the origin and size like: (CGRect){origin, size} and as a mixture: (CGRect){origin, {width, height}} What is the reason for not using this, and preferring the Make functions? 回答1: It isn't more efficient, actually. The CGRectMake function

Struct vs string literals? Read only vs read-write? [duplicate]

假如想象 提交于 2019-12-04 05:04:49
This question already has answers here : Closed 12 months ago . Why are compound literals in C modifiable (1 answer) Why do I get a segmentation fault when writing to a string initialized with “char *s” but not “char s[]”? (17 answers) 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 Approach, 2nd Edition on Page 406. Q. Allowing a pointer to a compound literal would seem to make it possible to modify the literal. Is that the case? A. Yes. Compound literals

Why use functions like CGRectMake?

你离开我真会死。 提交于 2019-12-03 10:57:06
I'm curious why functions like CGRectMake and CGPointMake exist and are widely used. when, instead, you can do: (CGRect){{x, y}, {width, height}} surely this is more efficient (though I'm guessing not by much) as there is no function call? Also you can set the origin and size like: (CGRect){origin, size} and as a mixture: (CGRect){origin, {width, height}} What is the reason for not using this, and preferring the Make functions? Kalle It isn't more efficient, actually. The CGRectMake function (and others) are declared as static inline , which means the compiler copy pastes the function code

Why doesn't compound literals assignment work without a typecast

☆樱花仙子☆ 提交于 2019-12-01 20:54:19
问题 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 a compound literal that is assigned to a struture. b = {5,6}; //doesn't work. b = (struct point){5,6}; //works. That doesn't seem to work without a typecast? What is the reason for this? 回答1: (struct point){5,6} as a whole is a compound literal. C11 §6.5

Why doesn't compound literals assignment work without a typecast

依然范特西╮ 提交于 2019-12-01 19:50:31
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 a compound literal that is assigned to a struture. b = {5,6}; //doesn't work. b = (struct point){5,6}; //works. That doesn't seem to work without a typecast? What is the reason for this? (struct point){5,6} as a whole is a compound literal. C11 §6.5.2.5 Compound literals A postfix expression that consists of a parenthesized type name followed by a brace

Array as compound literal [duplicate]

牧云@^-^@ 提交于 2019-11-30 22:46:30
This question already has an answer here: Why are compound literals in C modifiable 1 answer 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. It is an lvalue, we can see this if we look at the draft C99 standard section 6.5.2.5 Compound literals it says ( emphasis mine ):