100% Free Forever
AI-Powered Learning
Industry Expert Content
Certificates & Badges
Learn At Your Own Pace

C Pointers Cheat Sheet

C Pointers Cheat Sheet

Covers C pointer basics, pointer arithmetic with arrays, function pointers, malloc/free dynamic memory, and common undefined-behavior pitfalls.

2 PagesIntermediateMar 28, 2026

Pointer Basics

Declaring, addressing, and dereferencing pointers in C.

c
int x = 10;int *p = &x;         // p stores the address of xprintf("%d\n", *p);  // dereference: prints 10*p = 20;              // modifies x through pint *nullPtr = NULL;  // always initialize pointersif (nullPtr != NULL) {    printf("%d\n", *nullPtr);}printf("size of pointer: %zu\n", sizeof(p));  // typically 8 on 64-bit systems

Pointers & Arrays

Array names decay to pointers to their first element.

c
int arr[5] = {1, 2, 3, 4, 5};int *p = arr;              // equivalent to &arr[0]for (int i = 0; i < 5; i++) {    printf("%d ", *(p + i));  // same as arr[i]}// 2D array pointerint matrix[3][4];int (*rowPtr)[4] = matrix;    // pointer to an array of 4 intsprintf("%d\n", rowPtr[1][2]); // matrix[1][2]char *str = "hello";          // pointer to string literal (read-only)char buf[] = "hello";         // mutable copy on the stack

Pointers & Functions

Pass by pointer to modify caller data, and use function pointers.

c
void increment(int *p) {    (*p)++;}int x = 5;increment(&x);   // x is now 6// function pointerint add(int a, int b) { return a + b; }int (*opPtr)(int, int) = add;printf("%d\n", opPtr(2, 3));   // 5// array of function pointersint subtract(int a, int b) { return a - b; }int (*ops[2])(int, int) = { add, subtract };printf("%d\n", ops[1](5, 3));  // 2

Dynamic Memory (malloc/free)

Manual heap allocation with the C standard library.

c
#include <stdlib.h>int *arr = malloc(10 * sizeof(int));   // uninitialized memoryif (arr == NULL) {    // allocation failed, handle error}int *zeroed = calloc(10, sizeof(int)); // zero-initialized memoryarr = realloc(arr, 20 * sizeof(int));  // grow/shrink existing allocationfree(arr);free(zeroed);arr = NULL;   // avoid dangling pointer after free

Common Pitfalls

Mistakes that cause undefined behavior with C pointers.

  • Dangling Pointer- Pointer still used after the memory it points to was freed or went out of scope.
  • Memory Leak- Allocated memory (malloc/calloc) never freed, becoming unreachable.
  • NULL Dereference- Dereferencing a NULL or uninitialized pointer crashes the program (segfault).
  • Buffer Overflow- Writing past the end of an array through pointer arithmetic corrupts adjacent memory.
  • Double Free- Calling free() twice on the same pointer; undefined behavior, often heap corruption.
  • Pointer vs Array Confusion- sizeof(arr) gives the full array size, but sizeof(ptr) gives only the pointer's size (e.g. 8 bytes) once it has decayed.
Pro Tip

Always set a pointer to NULL immediately after free() - a freed-but-non-NULL pointer is a dangling pointer, and dereferencing or freeing it again is undefined behavior that NULL protects against, catching a lot of accidental double frees.

Was this cheat sheet helpful?

Explore Topics

#CPointers#CPointersCheatSheet#Programming#Intermediate#PointerBasics#PointersArrays#PointersFunctions#Dynamic#Functions#DataStructures#CheatSheet#SkillVeris
Advertisement
Sri Hayavadhana Info-Tech

Professional Web Designing Services

  • Responsive Websites
  • E-commerce Solutions
  • SEO Friendly Design
  • Fast & Secure
  • Support & Maintenance
Get a Free Quote

Share this Cheat Sheet