[comp.sys.mac.programmer] Color PixMap, the Clut and Think C Problem

darweesh@zephyrus.crd.ge.com (Michael Darweesh) (10/06/90)

Well, I'm trying to make a nice color offscreen pixmap almost exactly as
shown in Tech NOte #120, but with Think C 4.0.2.  I have copied that sample 
code almost exactly.  My problem is that on the line (in the tech note code)
that has the comment "/* Clone it*/" ( "it" in reference to the CTabHandle
involved in the HandToHand call on the same line), I get an Ivalue required
error when compiling.  I'm using prototypes and have the type-checking
option selected in the Think C options although I don't think they
should have much effect on this.  When I remove the "&" from that line and
try to compile, it works, but at run-time, I get and error (-111 or -113 I
believe).

Any help would be great.
-Mike Darweesh
weesh@crd.ge.com

sr0o+@andrew.cmu.edu (Steven Ritter) (10/07/90)

I ran into the same problem.  This seems to be a bug in the Think C
compiler.  If you code it as:
    err = HandToHand((Handle *)(&ourCMHandle));
instead of
    err = HandToHand(&((Handle)ourCMHandle));
it works, even though, to my eyes, the expressions should be the same.

russotto@eng.umd.edu (Matthew T. Russotto) (10/08/90)

In article <wb3WRoS00Uh_81DJdu@andrew.cmu.edu> sr0o+@andrew.cmu.edu (Steven Ritter) writes:
>I ran into the same problem.  This seems to be a bug in the Think C
>compiler.  If you code it as:
>    err = HandToHand((Handle *)(&ourCMHandle));
>instead of
>    err = HandToHand(&((Handle)ourCMHandle));

Excuse my K&R, but I don't even think that second one is legal C.
(typecasts and reference don't mix)

--
Matthew T. Russotto	russotto@eng.umd.edu	russotto@wam.umd.edu
      .sig under construction, like the rest of this campus.

dg@sisyphus.sybase.com (David Gould) (10/10/90)

In article <wb3WRoS00Uh_81DJdu@andrew.cmu.edu> sr0o+@andrew.cmu.edu (Steven Ritter) writes:
>I ran into the same problem.  This seems to be a bug in the Think C
>compiler.  If you code it as:
>    err = HandToHand((Handle *)(&ourCMHandle));
>instead of
>    err = HandToHand(&((Handle)ourCMHandle));
>it works, even though, to my eyes, the expressions should be the same.

This is not a bug, although one could reasonably argue that the compiler
should disallow it.  The two expressions are completely different in meaning,
and the second one does not make sense, so it is no suprise that it doesn't
work.

   (Handle *)(&ourCMHandle) means "take the address of ourCMHandle, and cast
				   the resultant pointer into a Handle pointer"

   &((Handle)ourCMHandle)   means "cast (ie possibly convert) ourCMHandle into
                                   a Handle, and then take the address of the
                                   result"

The problem with the second expression is that the result of the cast
'(Handle)ourCMHandle' is not an lvalue, and you cannot take the address of
or assign to anything that is not an lvalue.  That is, it does not have an
address or any defined defined storage.  For example, it might be in a
register.

						- dg

------  All opinions are mine and may or may not represent Sybase Inc.  ------
David Gould       dg@sybase.com        {sun,lll-tis,pyramid,pacbell}!sybase!dg
                  (415) 596-3414      6475 Christie Ave.  Emeryville, CA 94608