Path: csiph.com!news.swapon.de!eternal-september.org!feeder3.eternal-september.org!news.eternal-september.org!.POSTED!not-for-mail From: Keith Thompson Newsgroups: comp.lang.c Subject: Re: Casting the return value of ... Date: Thu, 28 Mar 2024 14:07:44 -0700 Organization: None to speak of Lines: 45 Message-ID: <87bk6yt68v.fsf@nosuchdomain.example.com> References: <20240328105203.773@kylheku.com> <87frwatadu.fsf@nosuchdomain.example.com> MIME-Version: 1.0 Content-Type: text/plain Injection-Date: Thu, 28 Mar 2024 21:07:44 +0100 (CET) Injection-Info: dont-email.me; posting-host="2dfbf40368bf600f73fcb17ce635941b"; logging-data="4006535"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX1+t7RYj4vSkDSZ0r065qfzU" User-Agent: Gnus/5.13 (Gnus v5.13) Emacs/27.2 (gnu/linux) Cancel-Lock: sha1:IXf5Q4oQTf/pvkKpYuCX9k9CU1s= sha1:4D8deAg8if7r9AkvtW6j2rWclqQ= Xref: csiph.com comp.lang.c:384071 bart writes: > On 28/03/2024 19:38, Keith Thompson wrote: >> Kaz Kylheku <433-929-6894@kylheku.com> writes: >> [...] >>> Conversions between function pointers and data pointers are an >>> extension; it is not well-defined behavior in ISO C. >>> >>> Therefore we can neither say that ISO C doesn't require a cast there (it >>> imposes no requirements at all), nor that the conversion is fine with a >>> cast. >>> >>> The cast is /likely/ necessary, in order to correctly trigger the >>> extension. >> ISO C does require a cast. The cast is necessary to avoid a >> constraint violation and a mandatory diagnostic. The resulting >> behavior is undefined in ISO C, but defined by POSIX. Assigning a >> void* value to a pointer-to-function object without a cast violates >> the constraint for simple assignment (N1570 6.5.16.1p1). > > What would such a cast look like? Since this gives a warning with > -Wpedantic even with a cast: > > void* p; > void(*q)(void); > > p=(void*)q; > q=(void(*)(void))p; The warnings I get from gcc are: warning: ISO C forbids conversion of function pointer to object pointer type [-Wpedantic] warning: ISO C forbids conversion of object pointer to function pointer type [-Wpedantic] With -pedantic-errors, these become fatal errors. I disagree with gcc. ISO C doesn't define the behavior, but it doesn't forbid the conversion. (Anyone who disagrees is invited to cite the constraint that it violates.) Note that clang doesn't issue this diagnostic. -- Keith Thompson (The_Other_Keith) Keith.S.Thompson+u@gmail.com Working, but not speaking, for Medtronic void Void(void) { Void(); } /* The recursive call of the void */