Path: csiph.com!eternal-september.org!feeder.eternal-september.org!nntp.eternal-september.org!.POSTED!not-for-mail From: Keith Thompson Newsgroups: comp.lang.c Subject: Re: function pointer question Date: Sat, 03 Jan 2026 16:39:14 -0800 Organization: None to speak of Lines: 56 Message-ID: <875x9i43f1.fsf@example.invalid> References: <10j7rs6$7c9e$1@dont-email.me> <20260102091518.226@kylheku.com> <10j96mn$jrsp$1@dont-email.me> <10j9enb$p6ts$2@dont-email.me> <10j9g71$pr9o$1@dont-email.me> <10jb3j5$17gcb$3@dont-email.me> MIME-Version: 1.0 Content-Type: text/plain Injection-Date: Sun, 04 Jan 2026 00:39:15 +0000 (UTC) Injection-Info: dont-email.me; posting-host="897b51646efb7ad5c94af47762914063"; logging-data="1740026"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX1+U8xRHfYCQB+VUJ6jiQU4E" User-Agent: Gnus/5.13 (Gnus v5.13) Cancel-Lock: sha1:EQkliTl07rEzmsSw3/JkoD5Lgpg= sha1:wpokOp0EDlLcqrdSiXG0NOBVFM8= Xref: csiph.com comp.lang.c:396130 David Brown writes: > On 02/01/2026 23:18, Chris M. Thomasson wrote: [...] >> typedef void (*generic_func_ptr)(void) > > There is no generic function pointer type equivalent to "void *" - you > are always going to need casts when converting between different > pointer types. And you /really/ need to make sure you convert to > exactly the correct type before calling the pointed-to function. But > the definition you gave here is commonly used when people want to have > generic function pointers. Yes, but ... There is no generic function pointer type equivalent to void* in the sense that values can be converted to and from the type implicitly. For example: int n = 42; void *vp = &n; // implicit conversion double *dp = vp; // implicit conversion, potentially dangerous There are no such implicit conversions for function pointer types. However, in the sense of a round-trip conversion yielding the original result *all* function pointer types can be treated as generic function pointer types. You just have to use casts to do any conversions. It's very common for all pointer types in a given implementation to have the same representation, and for round-trip conversions to safely yield the original value, but it's not guaranteed by the language, and there are implementations where, for example, function pointer types are bigger than object pointer types, or void* is bigger than int*. Certain collections of pointer types are guaranteed by the language to have the same representation: - void*, char*, signed char*, unsigned char* - All struct pointer types - All union pointer types - All function pointer types Incidentally, if I want a generic function pointer type, I might define it so that it can't be used accidentally without a cast. For example: typedef struct dummy__ (generic_function)(void); where struct dummy__ is an incomplete type. (Note that I've typedef'ed the function type, not the pointer type.) But that might be considered overkill; treating a void function with no parameters as generic is probably good enough. -- Keith Thompson (The_Other_Keith) Keith.S.Thompson+u@gmail.com void Void(void) { Void(); } /* The recursive call of the void */