[comp.sys.mac] rect.top vs: rect.topLeft in C

kdmoen@watcgl.UUCP (12/03/87)

tedj@hpcilzb.HP.COM (Ted Johnson) writes:
>Can someone tell me what I'm doing wrong here?  Using Lightspeed C v.2.11,
>I tried to do the following:
>
>		tempPoint = aRect.topLeft;

singer@endor.UUCP (Richard Siegal@THINK Technologies) writes:
>	You're getting confused between C and Pascal. In C there's no easy
>way to get at the variants of a record. Therefore, there's a macro (declared in
>"QuickDraw.h") for "topLeft" and "botRight". Instead of "aRect.topLeft", say
>"topLeft(aRect)", and all will be well.

Although I haven't seen this exact trick used in any Mac C environment
so far, it *is* possible to define Rect so that you can use the
syntax described in Inside Macintosh for accessing its fields:

typedef struct {
	Point topLeft;
	Point botRight;
} Rect;

#define top	topLeft.v
#define left	topLeft.h
#define bottom	botRight.v
#define right	botRight.h

Note that LightSpeed C defines top,left,bottom and right as fields
and topLeft,botRight as macros, while the above implementation does
the reverse, *and* gives you a cleaner syntax.
-- 
Doug Moen
University of Waterloo Computer Graphics Lab
UUCP:     {ihnp4,watmath}!watcgl!kdmoen
INTERNET: kdmoen@cgl.waterloo.edu

brian@ut-sally.UUCP (12/07/87)

In article <2571@watcgl.waterloo.edu>, kdmoen@watcgl.waterloo.edu (Doug Moen) writes:
} Although I haven't seen this exact trick used in any Mac C environment
} so far, it *is* possible to define Rect so that you can use the
} syntax described in Inside Macintosh for accessing its fields:
} 
} typedef struct {
} 	Point topLeft;
} 	Point botRight;
} } Rect;
} 
} #define top	topLeft.v
} #define left	topLeft.h
} #define bottom	botRight.v
} #define right	botRight.h
} 
} Note that LightSpeed C defines top,left,bottom and right as fields
} and topLeft,botRight as macros, while the above implementation does
} the reverse, *and* gives you a cleaner syntax.
} -- 
} Doug Moen
} University of Waterloo Computer Graphics Lab
} UUCP:     {ihnp4,watmath}!watcgl!kdmoen
} INTERNET: kdmoen@cgl.waterloo.edu

     The problem here is that you can no longer use variables with the names
top, left, bottom or right.  One of the old Megamax C's did something like
this as I recall.  (It wasn't necessarily this example.)  I wrote a program
in which I used a variable called "top" (using this as an example.)  We all
know how good most C compilers are at explaining the problem...
     I spent quite some time trying to figure out why

	int top;

     gave a "Syntax Error."

     Variables with the names top, left, bottom and right are much more common
in C programs than variables named topLeft, botRight, so that's why the macros
are done in a non-intuitive manner.

Brian H. Powell
		UUCP:	...!uunet!ut-sally!brian
		ARPA:	brian@sally.UTEXAS.EDU

   _Work_					 _Not Work_
  Department of Computer Sciences		P.O. Box 5899
  Taylor Hall 2.124				Austin, TX 78763-5899
  The University of Texas at Austin		(512) 346-0835
  Austin, TX 78712-1188
  (512) 471-9536

gae@osupyr.UUCP (Gerald Edgar) (12/08/87)

In article <9826@ut-sally.UUCP> brian@ut-sally.UUCP (Brian H. Powell) writes:

>     I spent quite some time trying to figure out why
>
>	int top;
>
>     gave a "Syntax Error."
>

(One version of) TML Pascal gives an error message when you use variables
called  h  or  v.  The way I program, these are even more frequent than top.




-- 
  Gerald A. Edgar                               TS1871@OHSTVMA.bitnet
  Department of Mathematics                     gae@osupyr.UUCP
  The Ohio State University  ...{akgua,gatech,ihnp4,ulysses}!cbosgd!osupyr!gae
  Columbus, OH 43210                            70715,1324  CompuServe