created: 20231029134919585 creator: Octt modified: 20231110234423621 modifier: Octt tags: title: C Language <<^wikipediaframe C_Language>> * [[reassign struct in C|https://stackoverflow.com/questions/10298070/reassign-struct-in-c]] (meaning reassigning all fields at a time); the feature is part of compound literals, sadly only supported in C99+, won't work before that and it's not easy to guess why without knowing this * [[Why do many functions that return structures in C, actually return pointers to structures?|https://softwareengineering.stackexchange.com/questions/359408/why-do-many-functions-that-return-structures-in-c-actually-return-pointers-to-s]] * [[Passing by reference in C|https://stackoverflow.com/questions/2229498/passing-by-reference-in-c]] --- "C does not support passing a variable by reference"... //Passing a pointer ''is'' passing-by-reference. This seems to be one of those facts that "savvy" C programmers pride themselves on. Like they get a kick out of it. "Oh you might THINK C has pass-by-reference but no it's actually just the value of a memory address being passed harharhar". Passing by reference literally just means passing the memory address of where a variable is stored rather than the variable's value itself [...]// * [[Pointers in C: when to use the ampersand and the asterisk?|https://stackoverflow.com/questions/2094666/pointers-in-c-when-to-use-the-ampersand-and-the-asterisk#2094715]] ** Related: [[error: invalid type argument of ‘unary *’ (have ‘int’)|https://stackoverflow.com/questions/5455866/error-invalid-type-argument-of-unary-have-int#5455962]], this happens when using `*` against a value instead of a pointer, since you can't dereference a value from a value. * [[What is a "callback" in C and how are they implemented?|https://stackoverflow.com/questions/142789/what-is-a-callback-in-c-and-how-are-they-implemented/142809#142809]] (function pointers) ** [['declared as a function' in C|https://stackoverflow.com/questions/23329261/declared-as-a-function-in-c#23329274]] --- function pointers in structs must be declared with the syntax `type (functionName*)( type arg1, ... )` * [[Where should I prefer pass-by-reference or pass-by-value?|https://stackoverflow.com/questions/4986341/where-should-i-prefer-pass-by-reference-or-pass-by-value]] --- (performance-wise) //"here's the simple rule: pass by reference when the value is large."// ** [[When should I pass or return a struct by value?|https://stackoverflow.com/questions/30980759/when-should-i-pass-or-return-a-struct-by-value]] * [[strcpy vs. memcpy|https://stackoverflow.com/questions/2898364/strcpy-vs-memcpy]] --- //strcpy stops when it encounters a NUL ('\0') character, memcpy does not//, aka as the names suggest often strcpy is perfect for strings while for generic data memcpy could be needed. * [[ISO C90 forbids mixed declarations and code in C|https://stackoverflow.com/questions/13291353/iso-c90-forbids-mixed-declarations-and-code-in-c]] --- old C standards required that new variables can be declared only before any other actual instruction in a scoped block, e.g. only at the top of a function * [["Multiple definition", "first defined here" errors|https://stackoverflow.com/questions/30821356/multiple-definition-first-defined-here-errors]] --- error that can arise in multiple cases * [[Creating empty function macros|https://stackoverflow.com/questions/9187628/empty-function-macros]] --- only safe way is `#define SomeFunction(arg) ((void)0)` ** [[C macros, what's the meaning of ((void)0)?|https://stackoverflow.com/questions/61157541/c-macros-whats-the-meaning-of-void0]] * [[How to use "else if" with the preprocessor #ifdef?|https://stackoverflow.com/questions/68696585/how-to-use-else-if-with-the-preprocessor-ifdef]] --- the only widely-supported way is: `#if defined(X)`...`#elif defined(Y)`...`#else`...`#endif` ** "compound conditionals" are supported, eg. `#if defined(X) || (defined(Y) && defined(Z))`... * [[How to enforce C89-style variable declarations in gcc?|https://stackoverflow.com/questions/3099813/how-to-enforce-c89-style-variable-declarations-in-gcc#3099874]] --- for some things of this goal, just the flags `-Wstrict-prototypes -Wmissing-prototypes -Wmissing-declarations -Wdeclaration-after-statement` are needed, I would guess.