[comp.sys.mac.programmer] LightSpeed C returning a pascal structure

iand@munnari.oz (Ian Robert Dobson) (11/23/88)

I am writing a program which utilises the list manager and have run into
problems using LightSpeed C (3.0).  The toolbox routine:
	   Function LLastClick (lHandle : ListHandle) : Cell;
would logically translate to:
	   pascal Cell LLastClick(ListHandle lHandle)

However LSC doesn't seem to want to return a non-integral pascal data type,
so the routine is defined in the #include file as returning a 'long'.
Furthermore, the type conversion between a long and a point gives me a 'bad
cast' error, even though they are the same size.  Does anybody have a solution
to obtaining the cell (point) without manually converting the types by
splitting the long in C code.

Ian R. Dobson
University of Melbourne
munnari.oz!iand

beard@ux1.lbl.gov (Patrick C Beard) (11/26/88)

In article <2583@munnari.oz> iand@munnari.oz (Ian Robert Dobson) writes:
>I am writing a program which utilises the list manager and have run into
>problems using LightSpeed C (3.0).  The toolbox routine:
>	   Function LLastClick (lHandle : ListHandle) : Cell;
>would logically translate to:
>	   pascal Cell LLastClick(ListHandle lHandle)
>
>However LSC doesn't seem to want to return a non-integral pascal data type,
>so the routine is defined in the #include file as returning a 'long'.
>Furthermore, the type conversion between a long and a point gives me a 'bad
>cast' error, even though they are the same size.  Does anybody have a solution
>to obtaining the cell (point) without manually converting the types by
>splitting the long in C code.
>
>Ian R. Dobson
>University of Melbourne
>munnari.oz!iand

Here's the hack I use:

	/* To convert a long to a Cell (which is really just a Point) */
	Cell theCell;
	long theLastClick;

	theLastClick=LLastClick(listH);

	theCell=*(Cell*)&theLastClick;	/* yech but it works! */

I hope this helps.

Patrick Beard
Lawrence Berkeley Laboratory

jcl@hpausla.HP.COM (Jeff Laing) (11/28/88)

In article <2583@munnari.oz> iand@munnari.oz (Ian Robert Dobson) writes:
>I am writing a program which utilises the list manager and have run into

I tend to use

union {
    Cell theCell;
    long clickVal;
} hack;

hack.clickVal = LLastClick(listH);

and then just access hack.theCell as normal.  Hopefully, Apple will never
change the definition of `Cell'.

siegel@endor.harvard.edu (Rich Siegel) (11/30/88)

In article <2583@munnari.oz> iand@munnari.oz (Ian Robert Dobson) writes:

>However LSC doesn't seem to want to return a non-integral pascal data type,
>so the routine is defined in the #include file as returning a 'long'.
>Furthermore, the type conversion between a long and a point gives me a 'bad
>cast' error, even though they are the same size.  Does anybody have a solution
>to obtaining the cell (point) without manually converting the types by
>splitting the long in C code.

	So, you want something for nothing? :-) It's not legal to cast a struct
to an integral type, or vice versa, but there are two ways to do it: the code
generated is identical, so it's strictly personal preference as to which you 
choose:

	Cell myCell;
	long myLong;

	1. 	myCell = *(Cell *)&myLong
	 2.	asm {move.l myLong, myCell}


	Solution (2) compiles in all cases, and solution 1 will only work
if "myLong" isn't a register variable. (You can't take the address of a
register variable.)

	--Rich




Rich Siegel
Staff Software Developer
THINK Technologies Division, Symantec Corp.
Internet: siegel@endor.harvard.edu
UUCP: ..harvard!endor!siegel
Phone: (617) 275-4800 x305

Any opinions stated in this article do not necessarily reflect the views
or policies of Symantec Corporation or its employees.