Relay-Version: version B 2.10 5/3/83; site utzoo.UUCP Path: utzoo!watmath!clyde!caip!rutgers!nike!ucbcad!ucbvax!sdcsvax!sdchem!tps From: tps@sdchem.UUCP (Tom Stockfisch) Newsgroups: net.lang.c Subject: Re: Casting void - int question Message-ID: <393@sdchema.sdchem.UUCP> Date: Tue, 14-Oct-86 22:44:20 EDT Article-I.D.: sdchema.393 Posted: Tue Oct 14 22:44:20 1986 Date-Received: Wed, 22-Oct-86 01:19:33 EDT References: <26@orion.UUCP> Reply-To: tps@sdchema.UUCP (Tom Stockfisch) Organization: Chemistry Dept, UC San Diego Lines: 64 In article <26@orion.UUCP> heins@orion.UUCP (Michael T. Heins) writes: > >int fna() { } > >void fnb() { } > >int (*array[32])(); >main() { > array[0] = fna; > array[1] = fnb; /* This won't work as-is. */ >} > >I have tried things like > array[1] = (int (*)())fnb; >but this generates the message "operands of CAST have incompatible types". The compiler rightly complains because void (*)() could conceivably be larger than int (*)() on some wierd machine. You should use a union, e.g.: union int_or_void_func { int (*int_func)(); void (*void_func)(); } array[32]; main() { array[0].int_func = fna; array[1].void_func = fnb; } The above is the much preferred way to do this. However, if you want to initialize "array[]", you currently can't use a union (ANSI standard may change this). Then the portable way to do this is int fna(); void fnb(); char *array[32] = { (char *)fna, (char *)fnb }; main() { ( *(int (*)())array[0] )(); /* call fna */ ( *(int (*)())array[1] )(); /* call fnb */ } If this won't work on some machines, I would appreciate hearing an explanation from some guru. A disadvantage of this method, besides ugliness, is that lint gives the complaint questionable conversion of function pointer for each element of array that you initialize, and each call as well. In any application where I use this method I wind up creating a lint filter such as grep -v 'questionable conversion of function pointer' and use it on all lint output. -- -- Tom Stockfisch, UCSD Chemistry