0
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?

5
  • 4
    What is the error message. What compiler? Commented Aug 29, 2017 at 15:12
  • 2
    There is a stray ` at the end of char name1[20],name2[20];` Commented Aug 29, 2017 at 15:14
  • ..I thought that was a speck of dust on the display:( Commented Aug 29, 2017 at 15:31
  • Yeah.. please post the code that you tested. Copy/paste it, don't type it in. It's too easy to make typos, eg. extraneous chars that will prevent compilation. Commented Aug 29, 2017 at 15:32
  • 1
    Also, post all the code you tested, ie. a MCVE. Non-MCVE plus typos gives the impression that you have just typed in the code from printed work and not actually tried it at all. Commented Aug 29, 2017 at 15:38

2 Answers 2

3

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.

Sign up to request clarification or add additional context in comments.

Comments

1

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.