In C, pointers can be compared using comparison operators such as ==
, !=
, <
, >
, <=
, and >=
. Pointer comparisons are useful when navigating through arrays or linked data structures and when determining whether two pointers point to the same or different locations in memory.
Comparison Operators with Pointers
The most commonly used comparison operators with pointers are:
- Equality (
==
): Checks if two pointers point to the same memory address. - Inequality (
!=
): Checks if two pointers point to different memory addresses. - Less than (
<
), Greater than (>
), Less than or equal to (<=
), Greater than or equal to (>=
): Used to compare pointers, typically when they point to elements in the same array.
Important Notes
- Pointer comparisons are meaningful when the pointers point to elements of the same array or contiguous memory block.
- If pointers point to unrelated objects (different memory allocations), the result of comparison using
<
,>
,<=
,>=
is undefined. - Comparison using
==
and!=
is valid for any pointers, as it simply checks whether the two pointers are pointing to the same address.
Example of Pointer Comparison
Consider an example involving an array and comparing pointers that point to different elements in the array.
#include <stdio.h>
int main() {
int arr[] = {10, 20, 30, 40, 50};
int *p1 = &arr[1]; // Points to the second element (arr[1] = 20)
int *p2 = &arr[4]; // Points to the fifth element (arr[4] = 50)
// Equality and Inequality
if (p1 == p2) {
printf("p1 and p2 point to the same memory address.\n");
} else {
printf("p1 and p2 do not point to the same memory address.\n");
}
// Output: p1 and p2 do not point to the same memory address.
// Less than and Greater than comparisons
if (p1 < p2) {
printf("p1 points to an earlier position in the array than p2.\n");
}
// Output: p1 points to an earlier position in the array than p2.
// Using pointer comparisons in a loop
int *start = arr; // Points to the first element (arr[0])
int *end = arr + 5; // Points one past the last element of the array
while (start < end) {
printf("%d ", *start); // Outputs the elements in the array
start++;
}
// Output: 10 20 30 40 50
return 0;
}
Explanation
Equality and Inequality (
==
,!=
):if (p1 == p2)
checks ifp1
andp2
point to the same address.- Since
p1
points toarr[1]
andp2
points toarr[4]
, they do not point to the same memory address, so the condition is false.
Relational Comparison (
<
,>
,<=
,>=
):if (p1 < p2)
checks ifp1
points to a memory location earlier thanp2
.- Since
p1
points to an element earlier in the array compared top2
, the condition is true. - Relational comparisons are valid here because
p1
andp2
are pointers to elements within the same array.
Using Pointers in Loops:
- A common usage of pointer comparison is in loops when traversing an array.
- In the
while (start < end)
loop, the loop runs untilstart
reachesend
(one past the last element of the array). - This technique is often used in situations where you need to access all elements of an array or until a certain pointer reaches a specified address.
Use Cases for Pointer Comparisons
- Array Traversal: Comparing pointers allows you to traverse through an array efficiently using
while
orfor
loops. - Boundary Checks: Pointer comparison is useful for ensuring a pointer doesn’t exceed the boundary of an allocated block of memory (such as with dynamic arrays).
- Linked List Traversal: When working with linked lists, you often compare pointers to traverse the list until a pointer reaches
NULL
.
Example: Linked List Traversal
Here's an example of using pointer comparisons with a linked list.
#include <stdio.h>
#include <stdlib.h>
// Define a linked list node
struct Node {
int data;
struct Node *next;
};
int main() {
// Create nodes
struct Node *head = malloc(sizeof(struct Node));
struct Node *second = malloc(sizeof(struct Node));
struct Node *third = malloc(sizeof(struct Node));
// Assign data and link nodes
head->data = 10;
head->next = second;
second->data = 20;
second->next = third;
third->data = 30;
third->next = NULL;
// Traverse and print the linked list
struct Node *current = head;
while (current != NULL) {
printf("%d ", current->data);
current = current->next;
}
// Output: 10 20 30
// Free allocated memory
free(head);
free(second);
free(third);
return 0;
}
Explanation:
- In the linked list, we use a pointer comparison (
!= NULL
) in thewhile
loop to traverse each node until reaching the end of the list. current != NULL
is used to ensure we continue looping as long ascurrent
points to a valid node.
Important Points to Remember
- Equality (
==
) and Inequality (!=
) comparisons can be performed between any pointers, regardless of whether they point to the same memory block. - Relational comparisons (
<
,>
,<=
,>=
) should only be performed on pointers that point to elements in the same array or contiguous block. - Comparing a pointer to
NULL
is a common practice to check if a pointer is uninitialized or if the end of a linked list has been reached. - Pointer comparison results are valid only if the pointers being compared point to memory locations within the same allocated block (e.g., same array).
Pointer comparison is a powerful tool that allows efficient control over memory, especially in contexts like array traversal and data structure navigation