c11

Aggregate initialization, set member pointer to same struct member

风流意气都作罢 提交于 2020-05-26 14:38:27
问题 Is it possible to use aggregate initialization to make a pointer aptr point to a which is a member of the same struct ? struct S { int a; int* aptr; }; int main() { S s = { .a = 3, .aptr = &a //point aptr to a }; return 0; } The question is for both C and C++ . 回答1: A working initialization would be: struct S { int a; int* aptr; }; int main() { struct S s = {.a = 3, .aptr = &s.a}; printf("%d", *s.aptr); } Working samples: C11 GNU C++2a GNU Regarding the correctness of the initialization: For

Is it possible to define _Generic's association-list dynamically?

烂漫一生 提交于 2020-05-13 18:48:25
问题 I have a template like this: template.h ---------- // Declare a function "func_type()" void JOIN(func_, T)(T t) { return; } #undef T which I use like this in order to generate the same function for different types: example.c --------- #define T int #include "template.h" #define T float #include "template.h" I would like to have a single func that I can use instead of funct_int , func_float , etc. My problem with _Generic is that it doesn't seem possible to define the association-list

Is it possible to define _Generic's association-list dynamically?

被刻印的时光 ゝ 提交于 2020-05-13 18:48:23
问题 I have a template like this: template.h ---------- // Declare a function "func_type()" void JOIN(func_, T)(T t) { return; } #undef T which I use like this in order to generate the same function for different types: example.c --------- #define T int #include "template.h" #define T float #include "template.h" I would like to have a single func that I can use instead of funct_int , func_float , etc. My problem with _Generic is that it doesn't seem possible to define the association-list

technical legality of incompatible pointer assignments

*爱你&永不变心* 提交于 2020-04-12 09:38:29
问题 The C11 standard ISO/IEC 9899:2011 (E) states the following constraints for simple assignments in §6.5.16.1/1: One of the following shall hold: the left operand has atomic, qualified, or unqualified arithmetic type, and the right has arithmetic type; the left operand has an atomic, qualified, or unqualified version of a structure or union type compatible with the type of the right; the left operand has atomic, qualified, or unqualified pointer type, and (considering the type the left operand

Declaring atomic pointers vs. pointers to atomics

大憨熊 提交于 2020-04-07 05:39:08
问题 I understand that the following declaration creates an array of values, each of which is atomic: _Atomic int x[10]; However, I'm unclear on whether this: _Atomic int *x; x = calloc(10, sizeof(int)); Creates an array of 10 atomic integers, or is an atomic pointer to an array of nonatomic integers. Does that syntax declare an array of atomics or an atomic pointer to an array, and whichever it is, how does one declare the other? (Note: I'm aware of atomic_int , and in the presented example it

Declaring atomic pointers vs. pointers to atomics

[亡魂溺海] 提交于 2020-04-07 05:38:10
问题 I understand that the following declaration creates an array of values, each of which is atomic: _Atomic int x[10]; However, I'm unclear on whether this: _Atomic int *x; x = calloc(10, sizeof(int)); Creates an array of 10 atomic integers, or is an atomic pointer to an array of nonatomic integers. Does that syntax declare an array of atomics or an atomic pointer to an array, and whichever it is, how does one declare the other? (Note: I'm aware of atomic_int , and in the presented example it

_Generic combined with variadic function?

拜拜、爱过 提交于 2020-03-19 07:49:11
问题 In C11, I could create a function which prototype would look like this: void myVaFunc(const char* const conv, ...); I could run it like this: myVaFunc("ici", 1, "test", 2); The function would know (after parsing the 1st parameter) that there are 3 additional parameters (4 with the initial one) with types consequently int , string (char pointer) and int . Easy, but not very elegant. Recently I have learned about the _Generic keyword, which allows to derive the type of a variable at the

_Generic combined with variadic function?

泪湿孤枕 提交于 2020-03-19 07:48:25
问题 In C11, I could create a function which prototype would look like this: void myVaFunc(const char* const conv, ...); I could run it like this: myVaFunc("ici", 1, "test", 2); The function would know (after parsing the 1st parameter) that there are 3 additional parameters (4 with the initial one) with types consequently int , string (char pointer) and int . Easy, but not very elegant. Recently I have learned about the _Generic keyword, which allows to derive the type of a variable at the

_Generic combined with variadic function?

こ雲淡風輕ζ 提交于 2020-03-19 07:47:32
问题 In C11, I could create a function which prototype would look like this: void myVaFunc(const char* const conv, ...); I could run it like this: myVaFunc("ici", 1, "test", 2); The function would know (after parsing the 1st parameter) that there are 3 additional parameters (4 with the initial one) with types consequently int , string (char pointer) and int . Easy, but not very elegant. Recently I have learned about the _Generic keyword, which allows to derive the type of a variable at the

Can unverified scanf call cause an undefined behavior?

拜拜、爱过 提交于 2020-01-30 06:08:46
问题 Does below snippet invoke undefined behavior in case of an error? #include <stdio.h> int main() { int i; /* Indeterminate */ if (scanf("%d", &i) == 1) /* Initialize */ printf("%d\n", i); /* Success! Print read value */ else printf("%d\n", i); /* Input failed! Is printing `i` UB or not? */ return 0; } What if scanf fails, is an uninitialized variable accessed? EDIT Moreover what if I replace scanf("%d", &i) with my_initializer(&i) : int my_initializer(int *pi) { double room_temp_degc = get