Pointers Tutorials
Programming
Pointers in C
A pointer is a special type of variable that stores the memory address of another variable.
Instead of storing a value directly, a pointer stores the location where the value is stored in memory.
Pointers are one of the most powerful features of C programming and are widely used for memory management, arrays, functions, structures, and dynamic memory allocation.
Why Do We Need Pointers?
Pointers provide direct access to memory locations.
They are useful for:
Dynamic memory allocation.
Passing arguments efficiently to functions.
Working with arrays and strings.
Creating data structures such as linked lists, trees, and graphs.
Optimizing memory usage and program performance.
Pointer Declaration
A pointer is declared using the asterisk (*) symbol.
Syntax
data_type *pointer_name;Example
int *ptr;Here:
intis the data type.ptris the pointer variable.*indicates thatptris a pointer.
This pointer can store the address of an integer variable.
Example of Pointer
#include <stdio.h>
int main()
{
int var = 10;
int *ptr = &var;
printf("%p", ptr);
return 0;
}Output
0x7fffffffe9ccThe output is a memory address represented in hexadecimal format.
Address Operator (&)
The address operator (&) is used to obtain the memory address of a variable.
Example
int num = 100;
printf("%p", &num);The address of num will be displayed.
Initializing a Pointer
A pointer should always be initialized before use.
Syntax
pointer_name = &variable;Example
int num = 50;
int *ptr = #Here, ptr stores the address of num.
NULL Pointer
If a pointer is not pointing to any valid memory location, it should be initialized with NULL.
Example
int *ptr = NULL;A NULL pointer indicates that the pointer currently points to nothing.
Dereferencing a Pointer
Dereferencing means accessing the value stored at the address contained in the pointer.
The dereference operator (*) is used for this purpose.
Example
#include <stdio.h>
int main()
{
int var = 10;
int *ptr = &var;
printf("%d", *ptr);
return 0;
}Output
10Understanding Pointer Declaration and Dereferencing
The * symbol has two different meanings.
Pointer Declaration
int *ptr;Here * declares a pointer variable.
Pointer Dereferencing
printf("%d", *ptr);Here * accesses the value stored at the address.
Pointer Memory Representation
int var = 10;
int *ptr = &var;Suppose:
Variable Value Address
var 10 1000
ptr 1000 2000Then:
ptr = 1000
*ptr = 10
&var = 1000
&ptr = 2000Printing Pointers
The %p format specifier is used to print memory addresses.
Example
printf("%p", ptr);Never use %d for printing pointer values.
Size of Pointers
The size of a pointer depends on the system architecture and not on the data type.
Example
#include <stdio.h>
int main()
{
int *ptr1;
char *ptr2;
printf("%zu\n", sizeof(ptr1));
printf("%zu\n", sizeof(ptr2));
return 0;
}Output (64-bit System)
8
8Output (32-bit System)
4
4All pointers have the same size on a particular system because they store memory addresses.
Types of Pointers in C
C provides several special types of pointers.
NULL Pointer
Void Pointer
Wild Pointer
Dangling Pointer
Constant Pointer
Function Pointer
Multilevel Pointer
NULL Pointer
A NULL pointer points to no valid memory location.
Example
#include <stdio.h>
int main()
{
int *ptr = NULL;
return 0;
}Void Pointer
A void pointer can store the address of any data type.
It is also called a generic pointer.
Example
#include <stdio.h>
int main()
{
void *ptr;
return 0;
}Example with Type Casting
int num = 100;
void *ptr = #
printf("%d", *(int *)ptr);Wild Pointer
A pointer that has not been initialized is called a wild pointer.
Example
int *ptr;The pointer contains a random memory address.
Using wild pointers may crash the program.
Dangling Pointer
A pointer that points to memory that has already been released is called a dangling pointer.
Example
#include <stdio.h>
#include <stdlib.h>
int main()
{
int *ptr = (int *)malloc(sizeof(int));
free(ptr);
ptr = NULL;
return 0;
}After free(), the pointer becomes dangling.
Assigning NULL removes the danger.
Pointer Arithmetic
Pointers support limited arithmetic operations.
Allowed operations:
Increment (
++)Decrement (
--)Addition of integer
Subtraction of integer
Comparison of pointers
Subtraction of pointers
Increment Pointer
int arr[] = {10, 20, 30};
int *ptr = arr;
ptr++;The pointer moves to the next element.
Example
#include <stdio.h>
int main()
{
int arr[] = {10, 20, 30};
int *ptr = arr;
printf("%d\n", *ptr);
ptr++;
printf("%d", *ptr);
return 0;
}Output
10
20Constant Pointer
A constant pointer always points to the same memory address.
Syntax
int *const ptr = &a;Example
int a = 10;
int b = 20;
int *const ptr = &a;This is valid:
*ptr = 50;This is invalid:
ptr = &b;The compiler generates an error because the address cannot be changed.
Pointer to Constant
The pointer can change its address, but the value cannot be modified through the pointer.
Syntax
const int *ptr;Example
const int *ptr = &a;Valid:
ptr = &b;Invalid:
*ptr = 50;Function Pointer
A function pointer stores the address of a function.
Example
#include <stdio.h>
int add(int a, int b)
{
return a + b;
}
int main()
{
int (*fptr)(int, int);
fptr = &add;
printf("%d", fptr(10, 5));
return 0;
}Output
15Multilevel Pointers
A pointer can point to another pointer.
The most common multilevel pointer is the double pointer.
Example
#include <stdio.h>
int main()
{
int var = 10;
int *ptr1 = &var;
int **ptr2 = &ptr1;
printf("%d\n", var);
printf("%d\n", *ptr1);
printf("%d", **ptr2);
return 0;
}Output
10
10
10Advantages of Pointers
Support dynamic memory allocation.
Efficiently access arrays and structures.
Enable call by reference.
Useful for memory manipulation.
Required for linked lists, trees, and graphs.
Improve performance in some situations.
Disadvantages of Pointers
Difficult for beginners to understand.
Improper use can cause program crashes.
May lead to memory leaks.
Uninitialized pointers cause errors.
Incorrect memory access can corrupt data.
Common Errors with Pointers
Using Uninitialized Pointer
int *ptr;
*ptr = 10;This may cause a crash.
Dereferencing NULL Pointer
int *ptr = NULL;
printf("%d", *ptr);This results in undefined behavior.
Using Dangling Pointer
free(ptr);
printf("%d", *ptr);Accessing freed memory is unsafe.
Summary
A pointer is a variable that stores the address of another variable.
The
&operator returns the address of a variable.The
*operator is used for declaration and dereferencing.%pis used to print memory addresses.All pointer types have the same size on a particular system.
Special pointer types include NULL, Void, Wild, Dangling, Constant, Function, and Multilevel pointers.
Pointer arithmetic allows limited mathematical operations on addresses.
Pointers are essential for dynamic memory allocation and advanced data structures.
Proper pointer handling is important to avoid memory-related errors.