What are the restrictions in comparing two pointers?

903 views Asked by At
int a=40,b=34;
int *iptr1,*iptr2;                        
iptr1 = &a;
iptr2 = &b;        
printf("\n Equal condition of two pointers=%d", (ip1 == ip2));    //no error 

char name1[20], name2[20];
char *p1 = name1;
char *p2 = name2;
if(p1 > p2) /*Error*/ 

Why there is an error/warning for the relation operation but none for the comparison operation?

2

There are 2 answers

0
Sourav Ghosh On

You can only perform relational operations ( <, >, <=, >= ) on pointers from the same array or same aggregate object. Otherwise, it causes undefined behavior.

Quoting C11, chapter ยง6.5.8, Relational operators, paragraph 5

When two pointers are compared, the result depends on the relative locations in the address space of the objects pointed to. If two pointers to object types both point to the same object, or both point one past the last element of the same array object, they compare equal. If the objects pointed to are members of the same aggregate object, pointers to structure members declared later compare greater than pointers to members declared earlier in the structure, and pointers to array elements with larger subscript values compare greater than pointers to elements of the same array with lower subscript values. All pointers to members of the same union object compare equal. If the expression P points to an element of an array object and the expression Q points to the last element of the same array object, the pointer expression Q+1 compares greater than P. In all other cases, the behavior is undefined.

In your code,

  if(p1>p2)

is an attempt to compare two pointers which are not part of the same array object, neither members of same aggregate object. So, it triggers the warning.

However, for comparison, no such constraint is held, so an expression like (ip1==ip2) is perfectly OK.

0
Vlad from Moscow On

You may compare pointers with the equality operators (== or !=) that point to different objects or elements of different arrays. If the pointers do not point to the same object then they are considered as unequal.

More precisely (the C Standard, 6.5.9 Equality operators)

6 Two pointers compare equal if and only if both are null pointers, both are pointers to the same object (including a pointer to an object and a subobject at its beginning) or function, both are pointers to one past the last element of the same array object, or one is a pointer to one past the end of one array object and the other is a pointer to the start of a different array object that happens to immediately follow the first array object in the address space.

Consider the following example.

#include <stdio.h>

int main(void) 
{
    struct A
    {
        int x;
        int y;
    } a;

    printf( "&a.x + 1 == &a.y is %d\n", &a.x + 1 == &a.y );

    return 0;
}

If there is no padding between the data members x and y of the structure then the output will be equal to 1 because each data member can be considered as an array with one element and the "array" y immediately follows the "array" x.

However you may not compare pointers with the relational operators (<, >, <=, >=) that do not point to elements of the same array or past the end of the same array.