Path: utzoo!attcan!uunet!husc6!think!ames!amdahl!ems!andrew From: andrew@ems.Ems.MN.ORG (Andrew C. Esh) Newsgroups: comp.sys.mac.programmer Subject: Re: Arrays of functions in THINK C Keywords: Why does this not work? Message-ID: <6509@ems.Ems.MN.ORG> Date: 8 Aug 88 02:00:29 GMT References: <2884@mit-amt.MEDIA.MIT.EDU> Reply-To: andrew@ems.Ems.MN.ORG (Andrew C. Esh) Organization: EMS/McGraw-Hill, Eden Pairie, MN Lines: 37 In article <2884@mit-amt.MEDIA.MIT.EDU> phil@mit-amt.MEDIA.MIT.EDU (Phil Sohn) writes: >typedef int (*GEN)(); > >int gen01(); > >GEN gensub[10]= {NULL, gen01} > >gensub[1](); <- compiler produces an error here > > phil@ems.media.mit.edu What? Don't listen to the other guys, they're blatherheads. First, you need a SEMICOLON after the array declaration. Second, what are you trying to do with the typedef? If all of the functions in the array will return an int, like gen01 does, then what you need is: int (*gensub[])() = {NULL, gen01}; /* semicolon, sir! */ (*gensub[1])(); /* This will call gen01, guaranteed */ Probably ought to change that NULL to a reference to a routine that simply returns a zero, or some other error value. If you do a "(*gensub[0])()", you'll get smoke and sparks (you'll start executing whatever is at zero, so your program will probably magically start over, if you're lucky). Notice also that I did not hardcode the size of the array. Since you're doing a static initialization right there, you may as well let the compiler count 'em up for you. It will woof cookies when you add the eleventh entry, otherwise. As for the rest of you, flame away. I'm fireproof on this one. I have the source (which has worked for 2 1/2 years now) right in front of my beady little eyes. :-) - Andrew "If it ain't fixed, don't broke it!" -- Andrew C. Esh DOMAIN: andrew@ems.MN.ORG APPLELINK: D0492 EMS/McGraw-Hill UUCP: ihnp4!meccts!ems!andrew AT&T: (612) 829-8200