Veda Bites are swipeable micro-lessons — each one teaches exactly one
idea. The bites below come from this kit — swipe through them in the app.
💡 Key Idea
What is a Pointer?
A variable that stores a memory address.
A pointer is a variable that holds the memory address of another variable. Instead of storing a value directly, it stores the location where the value is kept. This allows indirect access to data, enabling dynamic memory management and efficient array handling.
↳ Pointers store addresses, not values, enabling indirect data access.
📖 Definition
Pointer Declaration and Initialization
Declare with * and initialize with &.
A pointer is declared by specifying the type of data it points to, followed by an asterisk. It is initialized with the address of a variable using the `&` operator.
↳ Use `type *ptr;` to declare and `&variable` to initialize.
💡 Key Idea
Dereferencing a Pointer
Access the value at the address.
The dereference operator `*` is used to access the value stored at the address held by a pointer. This allows reading or modifying the original variable indirectly.
↳ `*ptr` gives the value at the address, enabling read and write.
⭐ Important Fact
Pointer Size and Type
Size is same, type matters.
On a given system, all pointers typically have the same size (e.g., 8 bytes on 64-bit), but the type determines how many bytes are accessed when dereferencing. An `int*` reads 4 bytes, while a `char*` reads 1 byte.
↳ Pointer size is uniform, but type defines dereference granularity.
🛠️ Application
Pointers and Arrays
Array name is a constant pointer.
In C, the name of an array acts like a constant pointer to its first element. This means array elements can be accessed using pointer arithmetic. For example, `arr[i]` is equivalent to `*(arr + i)`.
↳ Array indexing is syntactic sugar for pointer arithmetic.
⚠️ Common Mistake
Uninitialized Pointer
Dereferencing garbage causes crashes.
↳ Always initialize pointers to avoid crashes.
✏️ Example
Pointer to Pointer
Chain of addresses.
A pointer to a pointer stores the address of another pointer. It is useful for modifying pointer arguments in functions or creating dynamic 2D arrays.
↳ `**pp` dereferences twice to reach the final value.
⚙️ Process
Passing Pointers to Functions
Modify variables outside the function.
Passing a pointer to a function allows the function to modify the original variable, because it receives the address. This is called pass by reference in C.
↳ Pointer parameters enable functions to change caller's data.
➗ Formula
Pointer Arithmetic
Adding 1 moves by type size.
Pointer arithmetic adjusts the address by the size of the pointed-to type. For an `int*`, `ptr + 1` moves 4 bytes forward (assuming 4-byte int).
↳ Pointer increments scale by the size of the data type.
⚖️ Comparison
Pointers vs. Arrays
Similar but not identical.
↳ Arrays are fixed-size blocks; pointers are flexible references.
🌍 Real World
Dynamic Memory Allocation
Pointers enable runtime memory.
Functions like `malloc()` and `free()` use pointers to manage heap memory at runtime. This allows creating data structures like linked lists and dynamic arrays whose size is not known at compile time.
↳ Pointers are essential for dynamic memory management.
🎯 Exam Tip
NULL Pointer Check
Always check for NULL.
Before dereferencing a pointer, especially one returned by `malloc` or passed to a function, check if it is `NULL`. This prevents crashes and is a common exam question.
↳ Always validate pointers before use.
🧠 Memory Trick
Remember & and *
& is address, * is value.
Think of `&` as 'address of' and `*` as 'value at'. In declarations, `*` means 'pointer to'. This simple mnemonic helps avoid confusion.
↳ & gives address, * gives value.