Cheat Sheet - Linux: /linux/ - CLI: /linux/cli/ - locate vs type vs which vs whereis: /linux/cli/locate-vs-type-vs-which-vs-whereis/ - make: /linux/cli/make/ - awk: /linux/cli/awk/ - find: /linux/cli/find/ - grep: /linux/cli/grep/ - pgrep: /linux/cli/pgrep/ - ps: /linux/cli/ps/ - pstree: /linux/cli/pstree/ - trap: /linux/cli/trap/ - xargs: /linux/cli/xargs/ - Shell: /linux/shell/ - BASH_SOURCE: /linux/shell/bash-source/ - Redirection: /linux/shell/redirection/ - Special Variables: /linux/shell/special-variables/ - Scripting Tips: /linux/shell/script-tips/ - set -o vi: /linux/shell/bash-vi-mode/ - Shell Processing Order: /linux/shell/process-order/ - What are Interactive and Login Shells?: /linux/shell/interactive-login/ - Double Quote Expansion: /linux/shell/shell-expansion/ - System Programming: /linux/system-programming/ - File I/O: /linux/system-programming/file-io/ - Process: /linux/system-programming/process/ - Shared Libraries: /linux/system-programming/shared-libraries/ - Kernel: /linux/kernel/ - Go: /programming/go/ - Concurrency: /programming/go/concurrency/ - CSP (Communicating Sequential Processes) and Channels: /programming/go/concurrency/csp/ - Channel: /programming/go/concurrency/channel/ - Select: /programming/go/concurrency/select/ - sync: /programming/go/concurrency/sync/ - Concurrency Patterns: /programming/go/concurrency/patterns/ - Goroutines and the Go Runtime: /programming/go/concurrency/runtime/ - Standard Library: /programming/go/std/ - os: /programming/go/std/os/ - context: /programming/go/std/context/ - io: /programming/go/std/io/ - json: /programming/go/std/json/ - net/http: /programming/go/std/net-http/ - slog: /programming/go/std/slog/ - testing: /programming/go/std/testing/ - time: /programming/go/std/time/ - Ellipsis (...): /programming/go/ellipsis/ - error: /programming/go/error/ - Function: /programming/go/function/ - Go Tool: /programming/go/tool/ - Interface: /programming/go/interface/ - Map: /programming/go/map/ - Package and Module: /programming/go/package-module/ - Pointer: /programming/go/pointer/ - Slice: /programming/go/slice/ - String: /programming/go/string/ - Switch: /programming/go/switch/ - Comparison: /programming/go/comparison/ - defaultdict: /programming/python/collections/defaultdict/ - heapq: /programming/python/collections/heapq/ - @cache: /programming/python/cache/ - abc: /programming/python/abc/ - bisect: /programming/python/bisect/ - Type Hints: /programming/python/type-hints/ - Passing Mechanism: /programming/python/passing-mechanism/ - uv: /programming/python/uv/ - *args and **kwargs: /programming/python/args-kwargs/ - re: /programming/python/re/ - C: /programming/c/ - Pointers and Arrays: /programming/c/pointer/ - Syntax Basics: /programming/c/syntax-basics/ - static: /programming/c/static/ - Bit Manipulation: /programming/c/bit-manipulation/ - C vs C++: /programming/c/c-vs-cpp/ - stdatomic: /programming/c/stdatomic/ - C++: /programming/cpp/ - RAII (Resource Acquisition Is Initialization): /programming/cpp/raii/ - Copy Elision & Move Semantics: /programming/cpp/copy-elision-and-move-semantics/ - Most Vexing Parse & In-Class Initializer: /programming/cpp/most-vexing-parse/ - C++ Keywords Reference: /programming/cpp/keywords/ - C++ Primer: /programming/cpp/primer/ - Variables and Basic Types: /programming/cpp/primer/variables-and-basic-types/ - Expressions: /programming/cpp/primer/expressions/ - Type Conversions and Casts: /programming/cpp/primer/type-conversions-and-casts/ - Statements: /programming/cpp/primer/statements/ - Functions: /programming/cpp/primer/functions/ - Classes: /programming/cpp/primer/classes/ - The IO Library: /programming/cpp/primer/io-library/ - STL: /programming/cpp/primer/stl/ - Lambda Expressions: /programming/cpp/primer/lambda/ - Smart Pointers: /programming/cpp/primer/smart-pointers/ - Copy Control: /programming/cpp/primer/copy-control/ - Overloaded Operations and Conversions: /programming/cpp/primer/operator-overloading/ - Object-Oriented Programming (OOP): /programming/cpp/primer/oop/ - Templates and Generic Programming: /programming/cpp/primer/templates/ - Namespaces: /programming/cpp/primer/namespaces/ - Pointers to Class Member: /programming/cpp/primer/pointers-to-class-member/ - Advanced: /programming/cpp/advanced/ - Error Handling: /programming/cpp/advanced/error-handling/ - Concurrency & Threading: /programming/cpp/advanced/concurrency/ - Unit Testing: /programming/cpp/advanced/unit-testing/ - Comparison: /programming/comparison/ - Kubernetes: /cloud-devops/kubernetes/ - Fleet: /cloud-devops/kubernetes/gke/fleet/ - Kustomize vs Helm: /cloud-devops/kubernetes/kustomize-vs-helm/ - API Extension: /cloud-devops/kubernetes/api-extension/ - Concept: /cloud-devops/kubernetes/kueue/concept/ - DRA: /cloud-devops/kubernetes/dra/ - Secret Manager: /cloud-devops/terraform/google-secret-manager/ - Knowledge: /network/knowledge/ - PDU: /network/knowledge/osi-model/ - curl: /network/cli/curl/ - Concurrency Concepts: /cs/concurrency-and-parallelism/concepts/ - Concurrency vs Parallelism: /cs/concurrency-and-parallelism/concurrency-vs-parallelism/ - Mutex vs Atomic: /cs/concurrency-and-parallelism/mutex-vs-atomic/ - Regular Expression: /cs/regular-expression/ - Multiplexer vs Dispatcher: /cs/multiplexer-vs-dispatcher/ - Driver and Runtime: /ai-ml/gpu/driver-and-runtime/ - Serving: /ai-ml/serving/ - Dotfiles: /dotfiles/ - Git: /dotfiles/git/ - Cheat Sheet: /dotfiles/git/cheat-sheet/ - revisions: /dotfiles/git/revisions/ - Submodule vs Worktree: /dotfiles/git/submodule-vs-worktree/ - checkout, reset -> switch, restore: /dotfiles/git/checkout-switch-restore-reset/ - rebase: /dotfiles/git/rebase/ - Branching and Merging: /dotfiles/git/branching-merging/ - Vim: /dotfiles/vim/ - Plugin: /dotfiles/vim/plugin/ - auto-pairs: /dotfiles/vim/plugin/auto-pairs/ - vim-commentary: /dotfiles/vim/plugin/vim-commentary/ - vim-easymotion: /dotfiles/vim/plugin/vim-easymotion/ - vim-surround: /dotfiles/vim/plugin/vim-surround/ - Marks: /dotfiles/vim/tips/marks/ - vimrc: /dotfiles/vim/vimrc/ - lf: /dotfiles/lf/ # Pointers and Arrays ## Pointer and Array ### Pointer Decay In C, an array expression in most contexts automatically converts ("decays") into a pointer to its first element. - When an array is passed as an argument to a function, what is actually passed is a pointer to its first element. - The following function parameter declarations have identical signatures: ```c void f(int arr[]); void f(int arr[10]); void f(int *arr); // Adjusted signature ``` ### Multidimensional Array Parameters When passing a multidimensional array to a function, all dimensions except the first must be explicitly specified: ```c void Process2D(int arr[][4], int rows); ``` - **Reason:** The compiler requires the row size (column count) to perform pointer arithmetic. - **Address Calculation:** `arr[i][j]` is equivalent to `*(arr[i] + j)`, which expands to `*(*(arr + i) + j)`. - The expression `arr + i` advances memory by `i * sizeof(row)` (that is, `i * 4 * sizeof(int)`). ### Array vs. Pointer Operations - **Subscript Equivalence:** `arr[i]` is identical to `*(arr + i)`. - **Reassignment:** A pointer variable can be reassigned to point elsewhere, whereas an array identifier is not a modifiable lvalue and cannot be reassigned. - **Return Value Limitation:** A function cannot return an array directly; syntax such as `int f()[5]` is invalid in C. - **Workarounds:** Wrap the array inside a `struct` or pass the destination array as a pointer parameter. ```c struct ArrayWrapper { int data[5]; }; struct ArrayWrapper ReturnByValue(struct ArrayWrapper src) { return src; // Valid: structs can be copied and returned by value } ``` ### `char *` vs. `char[]` | Feature | `char *ptr = "hello";` | `char arr[] = "hello";` | | :--- | :--- | :--- | | **Type** | `char *` (pointer) | `char[6]` (array) | | **Memory Location** | Read-only data segment | Stack frame (copied from literal) | | **Element Modification** | Forbidden (Runtime crash / Segmentation fault) | Allowed (`arr[0] = 'H';`) | | **Reassignment** | Allowed (`ptr = "world";`) | Forbidden (Array name is not modifiable) | | **`sizeof` (64-bit)** | `8` (`sizeof(char *)`) | `6` (Includes terminating `\0`) | --- ## const Placement in Pointers The placement of `const` relative to the asterisk (`*`) determines what is immutable. Reading declarations from **right to left** resolves ambiguity. ### 1. `const T *ptr` (Pointer to Constant) - `const` appears to the **left** of `*` (equivalent to `T const *ptr`). - The **pointed-to data** is constant (low-level const). - Data modification is forbidden (`*ptr = value;`), but the pointer itself can be reassigned (`ptr = &other;`). ```c int x = 10, y = 20; const int *ptr = &x; *ptr = 15; // Compile error ptr = &y; // Valid ``` ### 2. `T *const ptr` (Constant Pointer) - `const` appears to the **right** of `*`. - The **pointer variable itself (the address)** is constant (top-level const). - Data modification is allowed (`*ptr = value;`), but the pointer cannot be reassigned (`ptr = &other;`). - Must be initialized upon declaration. ```c int x = 10, y = 20; int *const ptr = &x; *ptr = 15; // Valid ptr = &y; // Compile error ``` ### 3. `const T *const ptr` (Constant Pointer to Constant) - Both the pointer address and the pointed-to data are immutable. ```c int x = 10, y = 20; const int *const ptr = &x; *ptr = 15; // Compile error ptr = &y; // Compile error ``` --- ## Summary Comparison | Syntax | Description | Modify Value (`*ptr`) | Modify Address (`ptr`) | | :--- | :--- | :---: | :---: | | `int *ptr` | Mutable pointer to mutable data | Allowed | Allowed | | `const int *ptr` | Mutable pointer to constant data | Forbidden | Allowed | | `int *const ptr` | Constant pointer to mutable data | Allowed | Forbidden | | `const int *const ptr` | Constant pointer to constant data | Forbidden | Forbidden |