Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
What are different pointer operations and problems with pointers in C language?
A pointer is a variable whose value is the address of another variable, i.e., direct address of the memory location. Like any variable or constant, you must declare a pointer before using it to store any variable address.
Syntax
datatype *pointer_name;
Where datatype is the type of variable the pointer will point to, and pointer_name is the name of the pointer variable.
Memory Representation
Consider the following statement −
int qty = 179;
You can declare a pointer as follows −
int *p;
It means 'p' is a pointer variable that holds the address of another integer variable.
Different Pointer Operations
The pointer operations in C language are explained below −
- Assignment − We can assign an address to a pointer by using & (address operator).
- Value finding − It is nothing but dereferencing, where the * operator gives the value stored in the pointed to location.
- Taking a pointer address − Just like other variables, pointer variables have an address as well as a value.
- Adding an integer to a pointer − We can use + operator to add an integer to a pointer.
- Incrementing a pointer − Makes the pointer move to the next element of the array.
- Subtracting an int from a pointer − We use − operator to subtract an integer from a pointer.
- Decrementing a pointer − Points to the previous location.
- Subtraction − We can find the difference between two pointers.
- Comparison − We use relational operators to compare the values of two pointers.
Example: Basic Pointer Operations
The following program demonstrates the functioning of pointer operations in C language −
#include <stdio.h>
int main() {
int x, y;
int *p;
x = 10;
// Assigning address to pointer
p = &x;
y = *p;
printf("Value of x = %d
", x);
printf("x is stored at address %p
", (void*)&x);
printf("Value of x using pointer = %d
", *p);
printf("Address of x using pointer = %p
", (void*)p);
printf("Value of x in y = %d
", y);
*p = 25;
printf("Now x = %d
", x);
return 0;
}
Value of x = 10 x is stored at address 0x7fff5fbff6ac Value of x using pointer = 10 Address of x using pointer = 0x7fff5fbff6ac Value of x in y = 10 Now x = 25
Common Problems with Pointers
- Uninitialized pointers − Using pointers before assigning valid addresses leads to undefined behavior.
- Dangling pointers − Pointers pointing to memory that has been deallocated.
- Memory leaks − Forgetting to free dynamically allocated memory.
- Null pointer dereferencing − Attempting to access data through a NULL pointer.
- Buffer overflow − Writing beyond allocated memory boundaries.
Example: Pointer Problems
#include <stdio.h>
#include <stdlib.h>
int main() {
int *ptr;
// Problem 1: Uninitialized pointer
// *ptr = 10; // This would cause undefined behavior
// Correct way: Initialize before use
int value = 100;
ptr = &value;
printf("Correct usage: *ptr = %d
", *ptr);
// Problem 2: NULL pointer check
int *nullPtr = NULL;
if (nullPtr != NULL) {
printf("Safe to use: %d
", *nullPtr);
} else {
printf("NULL pointer detected, avoiding dereference
");
}
return 0;
}
Correct usage: *ptr = 100 NULL pointer detected, avoiding dereference
Conclusion
Pointer operations in C provide powerful memory management capabilities but require careful handling to avoid common pitfalls. Always initialize pointers, check for NULL values, and ensure proper memory management to write robust C programs.
