Back to C Programming
2026-01-078 min read

14.9 Pointer Comparison

Learn 14.9 Pointer Comparison step by step with clear examples and exercises.

Title: 14.9 Pointer Comparison in C Programming

Why This Matters

In C programming, pointers are used to manipulate memory directly. Understanding how to compare pointers is crucial for efficient memory management and debugging complex programs. This lesson will delve into the syntax and practical uses of pointer comparison in C, providing a comprehensive understanding of this essential concept.

Prerequisites

Before diving into pointer comparison, it's essential to have a solid understanding of:

  • Variables and data types
  • Pointers and their declaration
  • Pointer arithmetic
  • Dynamic memory allocation using malloc() and free() functions
  • Understanding the basics of arrays and pointers to arrays
  • Knowledge about structures and pointers to structures (optional but recommended)

Core Concept

Pointer comparison in C allows you to compare the memory addresses stored by two pointers. The comparison operators (==, !=, <, <=, >, >=) can be used with pointers, but Note that that they do not compare the values pointed to by the pointers. Instead, they compare the memory addresses themselves.

Pointer Comparison Rules

  1. Pointers of the same type can be compared for equality or inequality (==, !=).
  2. When comparing two pointers pointing to elements within the same array, they can be compared using relational operators (<, <=, >, >=). However, it's essential to remember that the comparison is based on the memory addresses of the elements and not their values.
  3. Pointers pointing to different arrays or data structures cannot be directly compared using relational operators. However, you can compare their base addresses if they are of the same type and size.
  4. Comparing pointers without checking if they point to valid memory locations can lead to undefined behavior, as the comparison may not yield the expected result or may cause a segmentation fault.
  5. When comparing pointers pointing to different data types, it's important to remember that their memory addresses will not necessarily be the same even if the values they point to are equivalent. In such cases, you can use typecasting to compare the pointers, but this should be done with caution and understanding of the potential issues.
  6. Comparing pointers without dereferencing them can lead to incorrect results as it compares memory addresses instead of the values pointed by the pointers. To compare the values pointed by the pointers, you should use *ptr1 == *ptr2.
  7. Comparing pointers pointing to out-of-bounds array elements can lead to undefined behavior, including segmentation faults and incorrect results.
  8. When comparing pointers in linked lists, it's essential to compare the data values of the nodes (not their memory addresses) using appropriate comparison functions like strcmp() for strings or custom comparison functions for other data types.

Worked Example

Let's consider a more complex example where we compare pointers in a dynamically allocated array and a linked list:

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

typedef struct Node {
char name[20];
int age;
struct Node *next;
} Node;

void printNode(Node *node) {
printf("Name: %s, Age: %d\n", node->name, node->age);
}

int compareNodes(Node *node1, Node *node2) {
return strcmp(node1->name, node2->name);
}

int main() {
int size = 5;
int *arr = (int *)malloc(size * sizeof(int));

for (int i = 0; i < size; ++i) {
arr[i] = i * 2;
}

int *ptr1 = &arr[0];
int *ptr2 = &arr[2];

if (ptr1 == ptr2) {
printf("Both pointers point to the same memory address.\n");
} else {
printf("Pointers point to different memory addresses.\n");
}

Node *head = NULL;
Node *newNode = malloc(sizeof(Node));
strcpy(newNode->name, "John Doe");
newNode->age = 30;
newNode->next = head;
head = newNode;

Node *node1 = head;
Node *node2 = head->next;
if (node1 == node2) {
printf("Both pointers point to the same memory address.\n");
} else {
printf("Pointers point to different memory addresses.\n");
}

if (compareNodes(node1, node2) == 0) {
printf("Nodes have the same name.\n");
} else {
printf("Nodes have different names.\n");
}

free(arr);
return 0;
}

In this example, ptr1 and ptr2 are pointers pointing to the first and third elements of the dynamically allocated array arr, respectively. Since they point to different memory addresses, the output will be "Pointers point to different memory addresses."

The linked list consists of two nodes with a custom comparison function compareNodes(). The output shows that the pointers node1 and node2 point to different memory addresses but their corresponding nodes have the same name.

Common Mistakes

  1. Comparing pointers without checking if they point to valid memory locations:
int *ptr = NULL; // ptr points to no memory location
if (ptr == &num1) { // This comparison will always be false!
printf("Pointers point to the same memory address.\n");
}
  1. Comparing pointers pointing to different data types:
int num = 5;
char *ptr = &num; // ptr points to an int, but it's declared as a char pointer
if (ptr == &num) { // This comparison will always be false!
printf("Pointers point to the same memory address.\n");
}
  1. Comparing pointers without dereferencing them:
int num1 = 5;
int *ptr1 = &num1;
int num2 = 10;
int *ptr2 = &num2;
if (ptr1 == ptr2) { // This comparison will always be false!
printf("Pointers point to the same memory address.\n");
}

In this example, ptr1 and ptr2 are pointers pointing to different variables, but they are compared without dereferencing them. So, the comparison will be based on their memory addresses instead of the values they point to. To compare the values pointed by the pointers, you should use *ptr1 == *ptr2.

  1. Comparing pointers pointing to out-of-bounds array elements:
int arr[5] = {0, 1, 2, 3, 4};
int *ptr1 = &arr[0];
int *ptr2 = &arr[6]; // Out of bounds!
if (ptr1 == ptr2) { // This comparison will always be false!
printf("Pointers point to the same memory address.\n");
}

In this example, ptr2 points to an out-of-bounds memory location, which may cause a segmentation fault or undefined behavior.

  1. Comparing pointers without considering their data types:
int *ptr1 = (int *)malloc(sizeof(int));
char *ptr2 = (char *)malloc(sizeof(char));
if (ptr1 == ptr2) { // This comparison is meaningless!
printf("Pointers point to the same memory address.\n");
}

In this example, ptr1 and ptr2 are pointers of different data types, so comparing them without considering their data types will not yield meaningful results.

Practice Questions

  1. Write a program that declares two arrays of different sizes and compares their starting addresses using pointers.
  2. Given a dynamically allocated array, write a function that returns a pointer to the middle element of the array. Compare this pointer with another pointer pointing to the last element of the array.
  3. Write a program that declares two arrays of the same size and compares their elements using pointers.
  4. Given a linked list, write a function that compares two nodes based on their data values (not memory addresses).
  5. Write a program that uses pointer comparison to find the minimum value in an array and its index.
  6. Write a program that uses pointer comparison to sort an array of integers using bubble sort algorithm.
  7. Write a program that uses pointer comparison to implement a binary search algorithm on a sorted array.
  8. Given two strings, write a function that determines whether they are anagrams by comparing their characters using pointers and without using built-in string functions like strcmp().
  9. Write a program that implements a simple memory allocator using pointer comparison to manage free and allocated memory blocks efficiently.
  10. Write a program that uses pointer comparison to implement a stack data structure with custom push, pop, and peek operations.

FAQ

Q: Can I compare two pointers if they point to different data types?

A: No, you cannot directly compare pointers pointing to different data types because their memory addresses will not necessarily be the same even if the values they point to are equivalent. However, you can use typecasting to compare them, but this should be done with caution and understanding of the potential issues.

Q: What happens when I compare two pointers without checking if they point to valid memory locations?

A: Comparing two pointers without checking if they point to valid memory locations can lead to undefined behavior, as the comparison may not yield the expected result or may cause a segmentation fault.

Q: Is it possible to compare pointers pointing to out-of-bounds array elements?

A: No, comparing pointers pointing to out-of-bounds array elements can lead to undefined behavior, including segmentation faults and incorrect results.

Q: Can I compare pointers without dereferencing them?

A: Yes, you can compare pointers without dereferencing them, but Note that that the comparison will be based on their memory addresses instead of the values they point to. To compare the values pointed by the pointers, you should use *ptr1 == *ptr2.

Q: What is the difference between comparing two pointers and comparing the values they point to?

A: Comparing two pointers checks whether their memory addresses are equal or not, while comparing the values they point to checks whether the values themselves are equal or not.

Q: Can I compare a pointer with an integer value?

A: No, you cannot directly compare a pointer with an integer value because they represent different types of data. However, you can convert the pointer to an integer (address) and then perform the comparison, but this should be done with caution and understanding of the potential issues.

Q: Can I compare pointers in a multidimensional array?

A: Yes, you can compare pointers in a multidimensional array by treating them as one-dimensional arrays and comparing their memory addresses accordingly.

Q: What is the purpose of pointer comparison in C programming?

A: Pointer comparison in C programming serves several purposes, including efficient memory management, debugging complex programs, and understanding the relationship between different variables and data structures. It helps developers identify issues like memory leaks, buffer overflows, and out-of-bounds access.