Loading content...
Loading content...
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.
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.
A pointer is declared using the asterisk (*) symbol.
data_type *pointer_name;int *ptr;Here:
int is the data type.
ptr is the pointer variable.
* indicates that ptr is a pointer.
This pointer can store the address of an integer variable.
#include <stdio.h>
int main()
{
int var = 10;
int *ptr = &var;
printf("%p", ptr);
return 0;
}0x7fffffffe9ccThe output is a memory address represented in hexadecimal format.
The address operator (&) is used to obtain the memory address of a variable.
int num = 100;
printf("%p", &num);The address of num will be displayed.
A pointer should always be initialized before use.
pointer_name = &variable;int num = 50;
int *ptr = #Here, ptr stores the address of num.
If a pointer is not pointing to any valid memory location, it should be initialized with NULL.
int *ptr = NULL;A NULL pointer indicates that the pointer currently points to nothing.
Dereferencing means accessing the value stored at the address contained in the pointer.
The dereference operator (*) is used for this purpose.
#include <stdio.h>
int main()
{
int var = 10;
int *ptr = &var;
printf("%d", *ptr);
return 0;
}10The * symbol has two different meanings.
int *ptr;Here * declares a pointer variable.
printf("%d", *ptr);Here * accesses the value stored at the address.
int var = 10;
int *ptr = &var;Suppose:
Variable Value Address
var 10 1000
ptr 1000 2000Then:
ptr = 1000
*ptr = 10
&var = 1000
&ptr = 2000The %p format specifier is used to print memory addresses.
printf("%p", ptr);Never use %d for printing pointer values.
The size of a pointer depends on the system architecture and not on the data type.
#include <stdio.h>
int main()
{
int *ptr1;
char *ptr2;
printf("%zu\n", sizeof(ptr1));
printf("%zu\n", sizeof(ptr2));
return 0;
}8
84
4All pointers have the same size on a particular system because they store memory addresses.
C provides several special types of pointers.
NULL Pointer
Void Pointer
Wild Pointer
Dangling Pointer
Constant Pointer
Function Pointer
Multilevel Pointer
A NULL pointer points to no valid memory location.
#include <stdio.h>
int main()
{
int *ptr = NULL;
return 0;
}A void pointer can store the address of any data type.
It is also called a generic pointer.
#include <stdio.h>
int main()
{
void *ptr;
return 0;
}int num = 100;
void *ptr = #
printf("%d", *(int *)ptr);A pointer that has not been initialized is called a wild pointer.
int *ptr;The pointer contains a random memory address.
Using wild pointers may crash the program.
A pointer that points to memory that has already been released is called a dangling pointer.
#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.
Pointers support limited arithmetic operations.
Allowed operations:
Increment (++)
Decrement (--)
Addition of integer
Subtraction of integer
Comparison of pointers
Subtraction of pointers
int arr[] = {10, 20, 30};
int *ptr = arr;
ptr++;The pointer moves to the next element.
#include <stdio.h>
int main()
{
int arr[] = {10, 20, 30};
int *ptr = arr;
printf("%d\n", *ptr);
ptr++;
printf("%d", *ptr);
return 0;
}10
20A constant pointer always points to the same memory address.
int *const ptr = &a;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.
The pointer can change its address, but the value cannot be modified through the pointer.
const int *ptr;const int *ptr = &a;Valid:
ptr = &b;Invalid:
*ptr = 50;A function pointer stores the address of a function.
#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;
}15A pointer can point to another pointer.
The most common multilevel pointer is the double pointer.
#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;
}10
10
10Support 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.
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.
int *ptr;
*ptr = 10;This may cause a crash.
int *ptr = NULL;
printf("%d", *ptr);This results in undefined behavior.
free(ptr);
printf("%d", *ptr);Accessing freed memory is unsafe.
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.
%p is 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.