pointer comparison question

steve emmerson steve at umigw.MIAMI.EDU
Thu Nov 10 00:06:08 AEST 1988


(My apologies if this has been covered before---as I'm sure it has :-)

I have a question on the validity and desireability of pointer comparisons 
in the following situation:

I have two arrays.  The elements of one array are structures and the
elements of the other need to be references to those structures.  These
references could be either pointers or indicies.  I wish to perform a
consistancy check on the references; in particular, I wish to insure
that they are valid (i.e. that the referred-to elements in do, in fact,
exist).  If indicies are used, I can simply perform range checking.

If pointers are used, however, can I verify the references?  My fear is
that I cannot because the relational comparison of pointers is
guaranteed to be valid only for pointers into the same array, and, in
this particular situation, that is exactly what I'm trying to
determine.  Put succinctly, is the following expression true:

    TestPtr < StartPtr || TestPtr > StopPtr

if, and only if, "TestPtr" points someplace outside the "StartPtr" 
to "StopPtr" range.

Must I use indicies and accept their potentially slower performance?

Thank you for your advice.
-- 
Steve Emmerson                     Inet: steve at umigw.miami.edu [128.116.10.1]
SPAN: miami::emmerson (host 3074::)      emmerson%miami.span at star.stanford.edu
UUCP: ...!ncar!umigw!steve               emmerson%miami.span at vlsi.jpl.nasa.gov
"Computers are like God in the Old Testament: lots of rules and no mercy"



More information about the Comp.lang.c mailing list