Path: utzoo!utgpu!jarvis.csri.toronto.edu!mailrus!ames!apple!oliveb!orc!mipos3!td2cad!brister From: brister@td2cad.intel.com (James Brister) Newsgroups: comp.lang.c Subject: Re: Typeof operator in C (Re: An Interesting View of "Strong" Vs. "Weak" Typing) Message-ID: Date: 13 Jan 90 02:20:57 GMT References: <16678@megaron.cs.arizona.edu> <7106@tank.uchicago.edu> <-K016ODxds13@ficc.uu.net> Sender: news@td2cad.intel.com Followup-To: comp.lang.c Distribution: usa Organization: Intel Corp., Santa Clara, CA, USA Lines: 43 In-reply-to: peter@ficc.uu.net's message of 12 Jan 90 18:58:35 GMT In article <-K016ODxds13@ficc.uu.net> peter@ficc.uu.net (Peter da Silva) writes: > But a typeof operator... wouldn't that be something... > > #define SWAP(a,b) {typeof a tmp; tmp=a; a=b; b=tmp} Check out the GNU CC compiler. It has a ``typeof'' keyword that looks like the ``sizeof'' keyword. It will take a type name or an expression and will return some object that can be used anywhere a typedef name could be used: e.g. typeof (*x) y declares y with the type of what x points to. typeof (*x) y[4] declares an array of such values so the SWAP macro should work. Just tried it and... Script started on Fri Jan 12 18:18:33 1990 Welcome to /bin/tcsh aries% cat test.c #define SWAP(a,b) {typeof (a) tmp; tmp=a; a=b; b=tmp;} int main () { int g=6 ,h=9 ; printf ("g = %d h = %d\n",g,h) ; SWAP (g,h) ; printf ("g = %d h = %d\n",g,h) ; } aries% gcc test.c aries% a.out g = 6 h = 9 g = 9 h = 6 aries% ^Dexit script done on Fri Jan 12 18:18:55 1990 -- James Brister brister@td2cad.intel.com Intel Corp. {decwrl,oliveb}!intelca!mipos3!td2cad!brister