A Pointer is one of the most powerful and distinctive features of the C language. While most variables store a value (like an integer or a character), a pointer is a variable that stores the memory address of another variable.
Pointers are the gateway to low-level memory manipulation, dynamic memory allocation, and efficient data handling in structures like linked lists and trees.
To understand pointers, you must understand how computer memory (RAM) works.
Think of RAM as a vast array of "mailboxes." Each mailbox:
Has a unique Address (usually a hexadecimal number like 0x7ffeb).
Holds a Value (the data stored inside).
When you declare int x = 10;, the computer finds an empty mailbox, labels it x, and puts the number 10 inside. A pointer to x would simply be a variable that stores the "mailbox number" (address) of x.
There are two fundamental operators you must master to work with pointers:
This unary operator returns the memory address of a variable.
Example: If x is at address 1000, then &x is 1000.
This operator is used in two ways:
In Declaration: To tell the compiler the variable is a pointer (e.g., int *ptr;).
In Execution: To access the value stored at the address the pointer is holding. If you "dereference" a pointer, you are looking inside the mailbox it points to.
A pointer must be declared to point to a specific data type. An int pointer should only point to int variables.
Syntax:
data_type *pointer_name;
Steps to use a pointer:
Define a variable (e.g., int a = 5;).
Define a pointer variable (e.g., int *p;).
Assign the address of the variable to the pointer (e.g., p = &a;).
This program demonstrates how to declare, assign, and use a pointer to modify a variable's value indirectly.
#include <stdio.h> int main() { int age = 25; // A regular variable int *ptr; // A pointer variable declaration ptr = &age; // Store the address of 'age' in 'ptr' printf("Value of age: %d\n", age); printf("Address of age (&age): %p\n", (void*)&age); printf("Value stored in ptr: %p\n", (void*)ptr); // Dereferencing the pointer to get the value printf("Value pointed to by ptr (*ptr): %d\n", *ptr); // Changing the value indirectly using the pointer *ptr = 30; printf("\nAfter modifying *ptr, new value of age: %d\n", age); return 0; }
Efficiency: Passing a large structure or array to a function by value creates a copy of the whole data, which is slow and wastes memory. Passing a pointer (the address) is extremely fast.
Dynamic Memory Allocation: Functions like malloc() return pointers. This allows you to request memory during runtime rather than deciding the size of your arrays at compile time.
Handling Hardware: In embedded systems, pointers are used to access specific hardware registers (memory-mapped I/O).
Multiple Returns: Since a function can only return one value, pointers allow you to pass multiple addresses to a function so it can "reach back" and update multiple variables in the caller.
| Type | Description |
| Null Pointer | A pointer assigned the value NULL or 0. It points to "nothing" and is used to prevent errors before a real address is assigned. |
| Void Pointer | A "generic" pointer (void *ptr) that can point to any data type. It must be type-casted before dereferencing. |
| Wild Pointer | A pointer that has been declared but not initialized. It points to a random memory location and is very dangerous. |
| Dangling Pointer | A pointer that points to a memory location that has been deleted or freed. |
Uninitialized Pointers: Never dereference a pointer without assigning it an address first. This usually leads to a Segmentation Fault.
Size of Pointers: Regardless of the data type they point to (char, int, double), all pointers in a specific system (32-bit or 64-bit) have the same size because they all store memory addresses.
Pointer Arithmetic: You can add or subtract integers from pointers (e.g., ptr++). This doesn't add 1 to the address; it moves the pointer to the next memory location based on the data type's size.
Copyright ©2025. All Rights Reserved Emblab THE RAVE INNOVATION