Relay-Version: version B 2.10 5/3/83; site utzoo.UUCP Posting-Version: version B 2.10.2 9/18/84; site loral.UUCP Path: utzoo!watmath!clyde!burl!ulysses!mhuxr!ihnp4!zehntel!zinfandel!hplabs!sdcrdcf!sdcsvax!sdcc6!loral!ian From: ian@loral.UUCP (Ian Kaplan) Newsgroups: net.lang,net.lang.pascal Subject: Re: Comparing pointers in Pascal Message-ID: <746@loral.UUCP> Date: Sat, 12-Jan-85 17:47:46 EST Article-I.D.: loral.746 Posted: Sat Jan 12 17:47:46 1985 Date-Received: Tue, 15-Jan-85 01:18:57 EST References: <3161@ucla-cs.ARPA> Organization: Loral Instrumentation, San Diego Lines: 81 Xref: watmath net.lang:1249 net.lang.pascal:189 > I just ran into a problem with pointers in Pascal: I have > two lists of pointers, and I need to find the intersection > of them. Due to the nature of the problem, it's cheaper to > keep these lists ordered. > > However, Pascal complains when I try to campare two pointer > variables with operators different than = and <>: > > [ Some more text which I have ommited ] > > I'm not trying to do arithmetic with pointers, I only need > to order them. I think this restriction is excessive. Is there > a good reason for it? Would Modula-2 behave in the same way? > Is it that Berkely Pascal is a wacko? > > Adolfo Berkeley Pascal is a fathful implementation of standard Pascal. You can get your code to work by using Pascal's infamous tagless case variant records. This sort of trick is commonly used by Pascal hackers to escape Pascal's type restrictions. A function like Equal below should work for pointer equality checks. It is not tested, so no guarantees. TYPE Pointer = ^Object; (* Function Equal returns TRUE if PtrOne is equal to PtrTwo, FALSE otherwise. *) FUNCTION Equal( PtrOne, PtrTwo : Pointer ) : BOOLEAN; (* Assuming that pointers occupy the same amount of memory as integers (an admittedly bad assumption on some systems) *) Trix = RECORD CASE INTEGER OF 1 : Ptr : Pointer; 2 : Int : INTEGER; END (* Case *) END; (* Record *) VAR One, Two : Trix; BEGIN One.Ptr := PtrOne; Two.Ptr := PtrTwo; Equal := One.Int = Two.Int; END; (* Equal *) Modula is designed for systems programming and provides much better facilities for type manipulation than Pascal. One of the standard Modula types is named CARDINAL. The CARDINAL type is used to represent unsigned integers. The type name can be used like a "cast" in C to tranform type. For example: TYPE Pointer = POINTER TO Object; (* Modula is case sensitive *) PROCEDURE Equal( PtrOne, PtrTwo : Pointer ) : BOOLEAN; BEGIN RETURN CARDINAL( PtrOne ) = CARDINAL( PtrTwo ); END Equal; The Modula standard procedure VAL could also be used: VAL( CARDINAL, PtrOne ) = VAL( CARDINAL, PtrTwo ) VAL is given two arguments, the destination type and the value to be transformed. VAL is usually safer to use since on some Modula implementations range checking is performed. Of course the Modula code is no more portable than the Pascal code, since its function is based on the assumption that pointers and CARDINALs are size compatible. Ian Kaplan Loral Data Flow Group Loral Instrumentation San Diego, CA (619) 560-5888 x4812 ucbvax!sdcsvax!sdcc6!loral!ian