Xref: utzoo comp.lang.c:35138 comp.std.c:4109 Path: utzoo!utgpu!news-server.csri.toronto.edu!bonnie.concordia.ca!thunder.mcrcim.mcgill.edu!snorkelwacker.mit.edu!usc!samsung!uunet!mcsun!unido!mikros!mwtech!martin From: martin@mwtech.UUCP (Martin Weitzel) Newsgroups: comp.lang.c,comp.std.c Subject: Re: (void *) to function pointer Message-ID: <1037@mwtech.UUCP> Date: 10 Jan 91 13:09:55 GMT References: <1991Jan9.000809.9152@brolga.cc.uq.oz.au> Reply-To: martin@mwtech.UUCP (Martin Weitzel) Organization: MIKROS Systemware, Darmstadt/W-Germany Lines: 46 In article <1991Jan9.000809.9152@brolga.cc.uq.oz.au> gottsc@citrus.citr.uq.oz.au (John Gottsc) writes: > >Hello, > >can anybody tell me what the ANSI C standards state regarding conversion >between a (void *) and a function pointer (i.e. is this type of >conversion possible in ANSI C)? No (as Doug Gwyn allready wrote). If you need a common storage place for data- and function pointers you have to use a union. If your function pointers are of different types (including prototype declarations of function parameters!) you either must specify all function types as components of the union, or use a cast before calling the function through the pointer. As there are no such things as "dynamic casts", i.e. all the casts are nailed down in the source, this normally means that there must be somewhere else some information which lets you determine the type of the function pointer at runtime and then you use a switch statement. This works because ANSI C requires that "all funtion pointers smell the same" - but only wrt their storage requirements. There may different subroutine linkage be used depending on the types of parameters, hence the correct cast is mandatory for portability. A less portable solution to transfer a function pointer to a `void *' is an intermediate cast to an `int'. The ANSI standard allows such casts but imposes no requirements% on the used mapping functions. So this may be used as a kluge if you only want to circumvent the limitation that a function pointer ca not be casted into a `void *' but don't expect this to work on an other hardware or even on the same hardware with some other memory modell or some other vendors compiler or the next release of your compiler. (Don't say I didn't warn you :-)) -------- %: A footnote in the relevant section of the standard (3.3.4) and some remarks in the rationale part can be read that it is "a quality of implementation issue" whether pointer1 -> integral type -> pointer2 result in pointer1 == pointer2. On segmented machine architectures it may further be possible, that *pointer1 and *pointer2 refer to the same object but do not compare equal. (All this is true for function pointers AND for data pointers.) -- Martin Weitzel, email: martin@mwtech.UUCP, voice: 49-(0)6151-6 56 83