Pointer Basics
Declare pointers, dereference, and walk an array with pointer arithmetic.
Code
#include <stdio.h>
int main(void) {
int x = 42;
int *p = &x; // pointer holds address of x
printf("x = %d\n", x);
printf("&x = %p\n", (void*)&x);
printf("p = %p\n", (void*)p);
printf("*p = %d\n", *p); // dereference
*p = 100; // modify x through pointer
printf("x now = %d\n", x);
int arr[3] = {10, 20, 30};
int *q = arr; // array decays to pointer
for (int i = 0; i < 3; i++) {
printf("arr[%d] = %d\n", i, *(q + i));
}
return 0;
}Explanation
A pointer stores the address of another variable, and the dereference operator * reads or writes the value at that address. Arrays decay to pointers when used in expressions, so pointer arithmetic can iterate over elements. Passing pointers to functions lets them mutate caller-owned data without copying it.
More C Snippets
Memory Management
Allocate, resize, and free heap memory with malloc, realloc, and free.
String Operations
Use string.h helpers for length, copy, concat, compare, and tokenize.
File I/O
Open, read, write, and close files using the stdio FILE API.
Structs
Group related fields with typedef and pass by pointer for mutation.
Function Pointers
Store function addresses for callbacks and dispatch tables.
Preprocessor Macros
Define object-like and function-like macros with conditional compilation.