How do the operators < and > work with pointers?
Posted
by
Øystein
on Stack Overflow
See other posts from Stack Overflow
or by Øystein
Published on 2011-01-05T17:53:10Z
Indexed on
2011/01/05
17:53 UTC
Read the original article
Hit count: 247
Just for fun, I had a std::list
of const char*
, each element pointing to a null-terminated text string, and ran a std::list::sort()
on it. As it happens, it sort of (no pun intended) did not sort the strings. Considering that it was working on pointers, that makes sense.
According to the documentation of std::list::sort()
, it (by default) uses the operator <
between the elements to compare.
Forgetting about the list for a moment, my actual question is: How do these (>, <, >=, <=) operators work on pointers in C++ and C? Do they simply compare the actual memory addresses?
char* p1 = (char*) 0xDAB0BC47;
char* p2 = (char*) 0xBABEC475;
e.g. on a 32-bit, little-endian system, p1
> p2
because 0xDAB0BC47
> 0xBABEC475
?
Testing seems to confirm this, but I thought it'd be good to put it on StackOverflow for future reference. C and C++ both do some weird things to pointers, so you never really know...
© Stack Overflow or respective owner