Path: utzoo!utgpu!news-server.csri.toronto.edu!cs.utexas.edu!uunet!huxley!steve From: steve@huxley.huxley.bitstream.com (Steve Stein) Newsgroups: comp.sys.mac.programmer Subject: Re: Known Symantec Think C 4.01 bug and work-around Message-ID: Date: 23 Oct 90 14:05:17 GMT References: <514@tci.UUCP> Sender: steve@huxley.UUCP Distribution: comp Organization: Bitstream, Inc. Lines: 68 In-reply-to: mitchell@tci.UUCP's message of 22 Oct 90 21:39:37 GMT In article <514@tci.UUCP> mitchell@tci.UUCP (Rob Mitchell) writes: > Subj: Known Symantec Think C 4.01 bug and work-around > BUG: LLastClick, of List Manager routines, returns a long instead > of a Cell type as defined in Inside Mac. > > BUGGY CODE: > ... Cell theCell; > ListHandle myList; > > /* compiler generates illegal > * union/struct operation > */ > theCell = LLastClick (myList); > In all fairness to Symantec, THIS IS NOT A BUG!!! (This is a "feature" of C.) C functions may not return structs (ref: Kernighan & Ritchie's "The C Programming Language", Appendix A ("C Reference Manual") Section 8.4: "functions may not return arrays, structures, unions or functions".) The problem arises from the fact that the Mac Toolbox is designed with Pascal interfaces (which may not exactly be the case nowadays). Pascal functions may return records ("struct"s in C). Luckily for C, this "record" (Cell) is only 4 bytes long, so C can return it in a register. It's just that the C language won't allow declaring LLastClick as returning the struct type Cell. The C language requires that LLastClick be declared as returning a long. > SOLUTION: Stick return of LLastClick into a long and cast > long to Cell type. > > SOLUTION CODE: > ... > Cell theCell; > ListHandle myList; > long tLong; > > tLong = LLastClick (myList); > theCell = *(Cell *) &tLong; I find it more convenient to use: union cell_return_type { Cell c; long n; } theCell; ListHandle myList; theCell.n = LLastClick(myList); and use "theCell.c" whenever I have to refer to the Cell's values. This is a bit kludgy, but it generates better code. > IN CLOSING: > > Symantec's Technial Support staff took all of 30 seconds to > analayze the problem and offer the solution. I'm really impressed > by the service and quality of Symantec. Hear, Hear!!!!! Technical support for the THINK languages has long been a true strength of THINK C and THINK Pascal. Disclaimer: I used to work for THINK and Symantec. - Steve Stein