C Pointers
What are Pointers in C?
A pointer in C is a special variable that stores the memory address of another variable instead of storing the actual value.
Pointers are one of the most powerful features of the C programming language. They are widely used for memory management, arrays, functions, dynamic memory allocation, and data structures.
By using pointers, programs become more efficient and flexible because they can directly access and manipulate memory locations.
Example
int age = 25;
int *ptr = &age;
Explanation
• age is an integer variable that stores the value 25.
• &age returns the memory address of the variable age.
• ptr is a pointer variable that stores the memory address of age.
• The * symbol is used to declare a pointer variable.
• The pointer can be used to access or modify the value stored at that memory location.
Why Pointers are Important?
Pointers are important because they:
• Allow direct access to memory locations.
• Improve program performance by passing addresses instead of copying data.
• Support dynamic memory allocation.
• Are essential for arrays, functions, and data structures.
• Help in efficient memory management.
Types of Pointers in C
C provides different types of pointers for various programming requirements.
The most commonly used pointer types are:
• Null Pointer
• Void Pointer
• Wild Pointer
Each type has its own purpose and usage in C Programming.
Null Pointer
A null pointer is a pointer that does not point to any valid memory location.
It is assigned the value NULL to indicate that it is currently not pointing to any object or variable.
Example
int *ptr = NULL;
Explanation
• ptr is declared as a pointer to an integer.
• NULL indicates that the pointer does not reference any memory location.
• Null pointers help prevent accidental access to invalid memory.
• They are commonly used to initialize pointer variables.
Void Pointer
A void pointer is a special type of pointer that can store the address of any data type.
Since it has no specific data type, it must be typecast before dereferencing.
Example
int age = 25;
void *ptr = &age;
Explanation
• ptr is declared as a void pointer.
• It stores the address of the integer variable age.
• A void pointer can point to any data type.
• Typecasting is required before accessing the value stored at the address.
Wild Pointer
A wild pointer is a pointer that has been declared but not initialized.
Using a wild pointer may lead to unpredictable results because it points to an unknown memory location.
Example
int *ptr;
Explanation
• ptr is declared but not initialized.
• It contains a garbage memory address.
• Accessing a wild pointer can cause unexpected program behavior.
• Always initialize pointers before using them.
Advantages of Pointers
• Provide direct access to memory locations.
• Improve program performance.
• Support dynamic memory allocation.
• Reduce memory usage by passing addresses.
• Essential for implementing data structures such as linked lists, trees, and graphs.
Example Program
Program
#include <stdio.h>
int main()
{
int age = 25;
int *ptr = &age;
printf(“Value of age = %d\n”, age);
printf(“Address of age = %p\n”, &age);
printf(“Value stored in pointer = %d\n”, *ptr);
return 0;
}
Output
Value of age = 25
Address of age = 0x…
Value stored in pointer = 25
Explanation
• age stores the value 25.
• ptr stores the memory address of age.
• %p is used to display the memory address.
• *ptr dereferences the pointer and accesses the value stored at that address.
• Pointers allow direct access to memory and improve program efficiency.
Key Points
• A pointer stores the memory address of another variable.
• The & operator returns the memory address of a variable.
• The * operator is used to declare and dereference pointers.
• Null pointers point to no valid memory location.
• Void pointers can point to any data type.
• Wild pointers are uninitialized pointers and should be avoided.
• Pointers are widely used in memory management and data structures.
