How to Compare Pointers in C: Syntax and Examples
In C, you compare pointers using relational operators like
==, !=, <, >, <=, and >=. These operators compare the memory addresses stored in the pointers, not the values they point to.Syntax
You can compare two pointers using these operators:
==checks if two pointers point to the same address.!=checks if two pointers point to different addresses.<,>,<=, and>=compare the relative positions of addresses in memory.
Example syntax:
if (ptr1 == ptr2) { /* same address */ }c
if (ptr1 == ptr2) { // pointers point to the same memory location } if (ptr1 != ptr2) { // pointers point to different memory locations } if (ptr1 < ptr2) { // ptr1 points to a lower memory address than ptr2 }
Example
This example shows how to compare two pointers to integers and print the result.
c
#include <stdio.h> int main() { int a = 5, b = 10; int *ptr1 = &a; int *ptr2 = &b; int *ptr3 = &a; if (ptr1 == ptr3) { printf("ptr1 and ptr3 point to the same address.\n"); } else { printf("ptr1 and ptr3 point to different addresses.\n"); } if (ptr1 != ptr2) { printf("ptr1 and ptr2 point to different addresses.\n"); } if (ptr1 < ptr2) { printf("ptr1 points to a lower memory address than ptr2.\n"); } else { printf("ptr1 points to a higher or equal memory address than ptr2.\n"); } return 0; }
Output
ptr1 and ptr3 point to the same address.
ptr1 and ptr2 point to different addresses.
ptr1 points to a lower memory address than ptr2.
Common Pitfalls
Common mistakes when comparing pointers include:
- Comparing pointer values instead of the data they point to. To compare data, dereference pointers with
*ptr. - Comparing pointers that do not belong to the same array or object can lead to undefined behavior when using relational operators like
<or>. - Assuming pointer comparison checks the content rather than the address.
c
#include <stdio.h> int main() { int a = 5, b = 5; int *ptr1 = &a; int *ptr2 = &b; // Wrong: compares addresses, not values if (ptr1 == ptr2) { printf("Pointers are equal (wrong assumption).\n"); } else { printf("Pointers are not equal (correct).\n"); } // Correct: compare values pointed to if (*ptr1 == *ptr2) { printf("Values pointed to are equal.\n"); } return 0; }
Output
Pointers are not equal (correct).
Values pointed to are equal.
Quick Reference
Tips for comparing pointers in C:
- Use
==and!=to check if pointers point to the same or different addresses. - Use relational operators only when pointers point within the same array.
- To compare the data, dereference pointers with
*. - Never compare pointers from unrelated objects with
<or>to avoid undefined behavior.
Key Takeaways
Use relational operators like == and != to compare pointer addresses directly.
Relational comparisons (<, >) are safe only for pointers within the same array.
To compare the actual data, dereference pointers with * before comparing.
Avoid comparing pointers from unrelated memory areas with < or > to prevent undefined behavior.
Pointer comparison checks addresses, not the content they point to.