Path: csiph.com!eternal-september.org!feeder.eternal-september.org!reader01.eternal-september.org!.POSTED!not-for-mail From: Keith Thompson Newsgroups: comp.lang.c Subject: Re: Abstract Syntax Tree for C programs Date: Tue, 04 Jun 2019 11:55:27 -0700 Organization: None to speak of Lines: 56 Message-ID: References: <475067fe-369c-4246-84a1-05b1a3a0c6be@googlegroups.com> <5dd4672f-a571-40bc-bb1a-c14f6c631eb7@googlegroups.com> Mime-Version: 1.0 Content-Type: text/plain; charset=us-ascii Injection-Info: reader02.eternal-september.org; posting-host="a9f7c3b5d2b438cdfd64a899e3119473"; logging-data="18072"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX19RL6Z3HH+hwkAriswdEy2V" User-Agent: Gnus/5.13 (Gnus v5.13) Emacs/23.1 (gnu/linux) Cancel-Lock: sha1:nd/qnqoAt6QESmL4iDgFmgg8m8A= sha1:0TBNSi1YukblPPwdHr9QC2tlqE4= Xref: csiph.com comp.lang.c:144421 Bart writes: > On 04/06/2019 07:56, Ike Naar wrote: >> On 2019-06-03, Bart wrote: >>> Using a void* to store a void(*)(void) is not a big deal, you don't want >>> -pedantic-errors to stop your build because of it. >> >> Using a function pointer (and not an object pointer) to store a >> function pointer isn't a big deal either. >> > > How would you create the following table: > > void fn1(void){} > void fn2(int a){} > void fn3(int a,double b){} > int fn4(void){return 0;} > char* fn5(double a){return "";} > > void* table[] = {fn1,fn2,fn3,fn4,fn5}; > > Suggestions to use void(*)(void) for the table element aren't as good. > > (Because you could actually write table[3]() without the compiler > picking that up. [...] I'd probably write it something like this: void fn1(void){} void fn2(int a){} void fn3(int a,double b){} int fn4(void){return 0;} char* fn5(double a){return "";} struct do_not_use { char dummy; }; typedef void generic_function(struct do_not_use); generic_function *const table[] = { (generic_function*)fn1, (generic_function*)fn2, (generic_function*)fn3, (generic_function*)fn4, (generic_function*)fn5, }; (I added the "const" to prevent accidental modifications of the elements. Remove it if you want to be able to modify them.) `table[0]()` is rejected with a "too few arguments" error. [...] -- Keith Thompson (The_Other_Keith) kst-u@mib.org Will write code for food. void Void(void) { Void(); } /* The recursive call of the void */