OcttKB/Wiki-OcttKB/tiddlers/Normal/_C Language.tid

39 lines
5.2 KiB
Plaintext
Raw Normal View History

2023-10-30 00:53:41 +01:00
created: 20231029134919585
creator: Octt
2023-12-24 18:45:03 +01:00
modified: 20231224151327804
2023-10-30 00:53:41 +01:00
modifier: Octt
tags:
title: C Language
<<^wikipediaframe C_Language>>
2023-11-07 00:58:12 +01:00
* [[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
2023-11-24 00:46:43 +01:00
* [[How to copy arrray to array using memcpy() in C|https://stackoverflow.com/a/15685912]] --- //you should allocate memory for tmp with size = sizeof(a). And then memcpy with size = sizeof(a)//
2023-12-13 01:33:15 +01:00
* [[Reading command line parameters|https://stackoverflow.com/questions/5157337/reading-command-line-parameters]]
2023-11-07 00:58:12 +01:00
2023-10-30 00:53:41 +01:00
* [[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 [...]//
2023-11-05 12:41:08 +01:00
* [[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.
2023-11-08 00:59:37 +01:00
* [[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)
2023-11-11 01:10:10 +01:00
** [['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, ... )`
2023-11-08 00:59:37 +01:00
* [[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]]
2023-12-24 18:45:03 +01:00
* [[Directly assigning values to C Pointers|https://stackoverflow.com/questions/17665793/directly-assigning-values-to-c-pointers]]
2023-11-05 12:41:08 +01:00
2023-11-03 01:23:03 +01:00
* [[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
2023-11-09 01:18:54 +01:00
* [["Multiple definition", "first defined here" errors|https://stackoverflow.com/questions/30821356/multiple-definition-first-defined-here-errors]] --- error that can arise in multiple cases
2023-11-06 00:29:21 +01:00
2023-11-04 01:53:01 +01:00
* [[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]]
2023-11-06 00:29:21 +01:00
* [[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`
2023-11-10 01:29:08 +01:00
** "compound conditionals" are supported, eg. `#if defined(X) || (defined(Y) && defined(Z))`...
2023-11-07 00:58:12 +01:00
* [[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.
2023-12-17 02:40:40 +01:00
* [[Good C-coding style for multiple lines if conditions|https://stackoverflow.com/questions/29328923/good-c-coding-style-for-multiple-lines-if-conditions]]
2023-11-27 00:43:43 +01:00
* [[How to get the current directory in a C program?|https://stackoverflow.com/questions/298510/how-to-get-the-current-directory-in-a-c-program]] --- (`getcwd(...)`)
2023-12-13 01:33:15 +01:00
* [[C Program to list all files and sub-directories in a directory|https://www.geeksforgeeks.org/c-program-list-files-sub-directories-directory/]]
* [[How to read the content of a file to a string in C?|https://stackoverflow.com/a/174552]]