Topics Covered Today
- Introduction to Pointers
- Declaring and Initializing Pointers
- Dereferencing Pointers
- Size of Pointers
- Special Types of Pointers
- Pointer Arithmetic
- Multilevel Pointers
- Practice Problems
Dr. Mohsin Dar
Assistant Professor, Cloud & Software Operations Cluster | SOCS | UPES
A pointer is a variable that stores the memory address of another variable.
Pointers are one of the most powerful features in C programming, allowing direct memory manipulation.
The asterisk (*) is used to declare a pointer variable.
The & operator returns the memory address of a variable.
The * operator (when used with a pointer) accesses the value stored at the address the pointer points to.
The size of a pointer depends on the architecture of the system, not the data type it points to.
A pointer that points to nothing. It's initialized with NULL value.
A generic pointer that can point to any data type.
Pointers that are not initialized and contain garbage values.
A pointer that points to freed or deallocated memory.
Pointers support the following arithmetic operations:
A pointer to pointer stores the address of another pointer.
| Concept | Description |
|---|---|
| Pointer | Variable that stores memory address |
| Declaration | data_type *ptr; |
| & Operator | Returns address of a variable |
| * Operator | Dereferences pointer (accesses value) |
| NULL Pointer | Points to nothing (safe initialization) |
| Void Pointer | Generic pointer (can point to any type) |
| Wild Pointer | Uninitialized pointer (dangerous!) |
| Dangling Pointer | Points to freed memory (dangerous!) |
| Pointer Arithmetic | +, -, ++, -- operations on pointers |
| Double Pointer | Pointer to pointer (**ptr) |
Write a program to swap two numbers using pointers. Create a function swap() that takes two integer pointers as parameters.
Write a program that uses pointer arithmetic to find the largest element in an integer array of size 10.
Create a program that uses a double pointer to dynamically allocate a 2D array of integers (3x3 matrix) and initialize it with values 1-9.
Write a function to reverse a string in-place using two pointers (one at the start and one at the end).
Write a program that declares three integer variables and three pointers. Initialize the pointers and compare their addresses to determine which variable is stored at the lowest memory address.
Create a generic print function using void pointer that can print integer, float, or character values based on a type parameter.