Typeof operator in C (Re: An Interesting View of "Strong" Vs. "Weak" Typing)

James Brister brister at td2cad.intel.com
Sat Jan 13 13:20:57 AEST 1990


In article <-K016ODxds13 at ficc.uu.net> peter at 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 at td2cad.intel.com
Intel Corp.                       {decwrl,oliveb}!intelca!mipos3!td2cad!brister



More information about the Comp.lang.c mailing list