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
How to compare pointers in C/C++?
Pointers in C can be directly compared using relational operators to check their memory addresses. This allows us to determine equality, ordering, and relative positions of pointers in memory.
Syntax
pointer1 == pointer2 // Equality comparison pointer1 != pointer2 // Inequality comparison pointer1 < pointer2 // Less than comparison pointer1 > pointer2 // Greater than comparison pointer1 <= pointer2 // Less than or equal pointer1 >= pointer2 // Greater than or equal
Pointer Comparison in C
In C, we can compare pointers using relational operators (==, !=, <, >, <=, >=). These operators compare the memory addresses stored in the pointers, not the values they point to. The comparison returns 1 (true) or 0 (false) based on the address relationship.
Example 1: Comparing Pointers to Different Variables
This example demonstrates comparing pointers that point to different variables −
#include <stdio.h>
int main() {
int a = 10, b = 20;
int *ptr1 = &a;
int *ptr2 = &b;
int *ptr3 = &a;
printf("Address of a: %p\n", (void*)ptr1);
printf("Address of b: %p\n", (void*)ptr2);
printf("Address of a (ptr3): %p\n", (void*)ptr3);
printf("ptr1 == ptr2: %s\n", (ptr1 == ptr2) ? "true" : "false");
printf("ptr1 == ptr3: %s\n", (ptr1 == ptr3) ? "true" : "false");
printf("ptr1 != ptr2: %s\n", (ptr1 != ptr2) ? "true" : "false");
if (ptr1 < ptr2) {
printf("ptr1 points to a lower address than ptr2\n");
} else {
printf("ptr1 points to a higher address than ptr2\n");
}
return 0;
}
Address of a: 0x7fff5fbff6bc Address of b: 0x7fff5fbff6b8 Address of a (ptr3): 0x7fff5fbff6bc ptr1 == ptr2: false ptr1 == ptr3: true ptr1 != ptr2: true ptr1 points to a higher address than ptr2
Example 2: Comparing Array Element Pointers
When comparing pointers to array elements, the comparison follows the array's memory layout −
#include <stdio.h>
int main() {
int arr[5] = {1, 2, 3, 4, 5};
int *ptr1 = &arr[1];
int *ptr2 = &arr[3];
printf("ptr1 points to arr[1], ptr2 points to arr[3]\n");
printf("ptr1 == ptr2: %s\n", (ptr1 == ptr2) ? "true" : "false");
printf("ptr1 < ptr2: %s\n", (ptr1 < ptr2) ? "true" : "false");
printf("ptr2 > ptr1: %s\n", (ptr2 > ptr1) ? "true" : "false");
/* Calculate distance between pointers */
printf("Distance between pointers: %ld elements\n", ptr2 - ptr1);
return 0;
}
ptr1 points to arr[1], ptr2 points to arr[3] ptr1 == ptr2: false ptr1 ptr1: true Distance between pointers: 2 elements
Key Points
- Pointer comparison compares memory addresses, not the values being pointed to.
- Comparing pointers to different objects may yield unpredictable results depending on memory layout.
- Comparison is meaningful when pointers reference elements of the same array or object.
- NULL pointer comparison is safe:
ptr == NULLorptr != NULL.
Conclusion
Pointer comparison in C uses standard relational operators to compare memory addresses. It's most useful for array elements and checking for NULL pointers, providing essential functionality for memory management and data structure operations.
