Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Pointer comparison

Tags:

c++

c

pointers

Do pointers in C and C++ support comparison operators (>, <, etc.) in standard?

I want to compare array positions to be precise.

like image 994
fsdemir Avatar asked Dec 13 '22 02:12

fsdemir


2 Answers

In a contiguous array comparing memory offsets (pointers) is OK. If your array is implemented as a linked list (for example) the nodes could be all over memory so pointer comparison is nonsensical.

like image 92
fbrereto Avatar answered Dec 31 '22 23:12

fbrereto


Yes, they can be compared.

For example, see "Relational Operators" in standards for further information, 6.5.8 in C99, and 5.9 in old draft of C++ (2006-11).

like image 20
maykeye Avatar answered Dec 31 '22 22:12

maykeye