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?
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 5In your code,
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.