[comp.sys.mac] Help wanted: a LSC question

tedj@hpcilzb.HP.COM (Ted Johnson) (11/27/87)

Can someone tell me what I'm doing wrong here?  Using Lightspeed C v.2.11,
I tried to do the following:

	Rect aRect;
	Point tempPoint;

		SetRect(&aRect, 3, 3, 300, 300);
		tempPoint = aRect.topLeft;

When I tried to compile this, I got the error message: 
	"Wrong number of arguments to macro topLeft"

According to I.M. I-141, a Rect is defined as:

	Type Rect = RECORD CASE INTEGER OF
		0: (top:     INTEGER;
		    left:    INTEGER;
 		    bottom:  INTEGER;
		    right:   INTEGER);
		1: (topLeft: Point;
		    botRight: Point)
		END;

What am I doing wrong?!?  Any clues would be greatly appreciated!

	-Ted

*******************************************
Ted Johnson
Hewlett-Packard, Design Technology Center
Santa Clara, CA
(408)553-3555
UUCP:  ...hplabs!hpcea!hpcilzb!tedj
*******************************************

	

singer@endor.harvard.edu (THINK Technologies) (12/01/87)

In article <870053@hpcilzb.HP.COM> 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;
>
>When I tried to compile this, I got the error message: 
>	"Wrong number of arguments to macro topLeft"

	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.

		--Rich

**The opinions stated herein are my own opinions and do not necessarily
represent the policies or opinions of my employer (THINK Technologies).

* Richard M. Siegel | {decvax, ucbvax, sun}!harvard!endor!singer    *
* Customer Support  | singer@endor.harvard.edu			    *
* Symantec, THINK Technologies Division.  (No snappy quote)         *

dorner@uxc.cso.uiuc.edu (12/02/87)

>>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;
>>
>>When I tried to compile this, I got the error message: 
>>	"Wrong number of arguments to macro topLeft"
>
>	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.
>
>		--Rich

Ahem.

typedef struct
    {
	union {
	    int coords[4];
	    Point funnies[2];
	} r_un;
    } Rect ;
#define top		r_un.coords[0]
#define left		r_un.coords[1]
#define bottom		r_un.coords[2]
#define right		r_un.coords[3]
#define topLeft		r_un.funnies[0]
#define botRight	r_un.funnies[1]

This would have worked fine, and allowed me to say r.topLeft.

Perhaps (I'm not well versed (yet) in the ins and outs of Macintosh
programming) there are reasons that this was not done.  But to chalk it
up to a deficiency in C is not correct.
----
Steve Dorner, U of Illinois Computing Services Office
Internet: dorner@uxc.cso.uiuc.edu  UUCP: ihnp4!uiucuxc!dorner
IfUMust:  (217) 333-3339

howard@cpocd2.UUCP (12/03/87)

>In article <870053@hpcilzb.HP.COM> 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;
>>
>>When I tried to compile this, I got the error message: 
>>	"Wrong number of arguments to macro topLeft"

In article <3424@husc6.harvard.edu> singer@endor.UUCP (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.

Assuming something like:

	typedef struct {int x, y;} point;

one way of describing a rectangle which would allow easy access is:

	typedef union
	{
		struct {int top, left, bottom, right;}	ints;
		struct {point topLeft, bottomRight;}	points;
	} rectangle;

which would allow references of the form:

	tempPoint = aRect.points.topLeft;
	tempInt = aRect.ints.top;

Note that this doesn't require the overhead of an extra variable to
keep track of variants.  Admittedly, Ted got confused, but that's no
reason to make incorrect accusations about C.

-- 
	Howard A. Landman
	{oliveb,hplabs}!intelca!mipos3!cpocd2!howard
	howard%cpocd2.intel.com@RELAY.CS.NET
	"I'm sorry, Dave, but I can't do that."

steele@unc.UUCP (12/04/87)

dorner@uxc.cso.uiuc.edu writes:
>[....]
>typedef struct
>    {
>	union {
>	    int coords[4];
>	    Point funnies[2];
>	} r_un;
>    } Rect ;
>#define top		r_un.coords[0]
>#define left		r_un.coords[1]
>#define bottom		r_un.coords[2]
>#define right		r_un.coords[3]
>#define topLeft	r_un.funnies[0]
>#define botRight	r_un.funnies[1]
>
>This would have worked fine, and allowed me to say r.topLeft.

It would also have caused my code that says
	int top, left, bottom, right;
to break in bizarre ways that would have been even more difficult to find
than the r.topLeft problem was (for me, at least).  Granted,
	Point	topLeft;
breaks anyway, but if I'm a representative coder then that rarely
comes up.

>Perhaps (I'm not well versed (yet) in the ins and outs of Macintosh
>programming) there are reasons that this was not done.  But to chalk it
>up to a deficiency in C is not correct.

It's not a deficiency in C, but it is a situation where C's syntax is
different from Pascal.  Since the definitions are specified in Pascal,
then C programming on the Mac is arguably less well documented than is
Pascal programming, and this is a deficiency of the C environment.

------------------------------------------------------------------------------
Oliver Steele				  ...!{decvax,ihnp4}!mcnc!unc!steele
							steele%unc@mcnc.org

	"Life isn't fair.  It's just fairer than death, that's all."
							-- William Goldman
							  _The Princess Bride_

singer@endor.harvard.edu (THINK Technologies) (12/04/87)

In article <174400081@uxc.cso.uiuc.edu> dorner@uxc.cso.uiuc.edu writes:
>

>This would have worked fine, and allowed me to say r.topLeft.
>
>Perhaps (I'm not well versed (yet) in the ins and outs of Macintosh
>programming) there are reasons that this was not done.  But to chalk it
>up to a deficiency in C is not correct.

	Um, you're right, and I stand corrected. (Open mouth, insert foot.)

:-)

		-_Rich
**The opinions stated herein are my own opinions and do not necessarily
represent the policies or opinions of my employer (THINK Technologies).

* Richard M. Siegel | {decvax, ucbvax, sun}!harvard!endor!singer    *
* Customer Support  | singer@endor.harvard.edu			    *
* Symantec, THINK Technologies Division.  (No snappy quote)         *

gardner@prls.UUCP (12/04/87)

In article <1005@cpocd2.UUCP> howard@cpocd2.UUCP (Howard A. Landman) writes:
>>In article <870053@hpcilzb.HP.COM> tedj@hpcilzb.HP.COM (Ted Johnson) writes:
>>>I tried to do the following:

>>>		tempPoint = aRect.topLeft;

>one way of describing a rectangle which would allow easy access is:

>	typedef union
>	{
>		struct {int top, left, bottom, right;}	ints;
>		struct {point topLeft, bottomRight;}	points;
>	} rectangle;

>which would allow references of the form:

>	tempPoint = aRect.points.topLeft;
>	tempInt = aRect.ints.top;

Megamax did this. It's a pain. I was very relieved to see LSC's method
of defining Rect's. It's much more convenient to use, especially since
you access top,left,bottom,right MUCH more frequently than topLeft or
botRight.
By the way, Pascal's definition of Point is a union, also, with one
union giving two ints and the other an array of 2 ints. So accessing
the fields of a point also required an extra union-specifier with Megamax.
Some programmers may like this. I personally find this to be one of 
the more obnoxious aspects of C. I like LSC's approach.

Robert Gardner