Casting void - int question

jnm at Sun.COM jnm at Sun.COM
Thu Oct 16 10:03:56 AEST 1986


Interestingly, you second example, using 
    array[1] = (int(*)())fnb;
 works fine on my machine (A Sun Workstation)...

Here is some code I wrote to achieve what you were trying to do
using alternative methods which may be useful.

I included the typedef's to make things more legible...

1.  The following program compiles ...


int fna() { }
 
void fnb() { }

typedef int (*pfi)();
typedef void (*pfv)();

pfi array[32];
main() {
    array[0] = fna;
    array[1] = (pfi)fnb;
}


2. Use Unions instead of casting ...

int fna() { }
 
void fnb() { }

typedef int (*pfi)();
typedef void (*pfv)();

struct 
{ union {
    pfi fnint;
    pfv fnvoid;
    } Union;
} array[32];
main() {
    array[0].Union.fnint = fna;
    array[1].Union.fnvoid = fnb;
}

Hope that's useful

Jonathan
sun!sunuk!jmills



More information about the Comp.lang.c mailing list