Solution of array of pointers to functions problem

William Davidsen davidsen at sungod.crd.ge.com
Thu Jun 15 02:37:35 AEST 1989


In article <823 at helios.toronto.edu> dooley at helios.physics.utoronto.ca (Kevin Dooley) writes:

| Steve points out that the typedef is critical, ie
| 
| 	double	(*functionList[])() = { ... };
| 
| *DOES**NOT**WORK*.  This is the peculiarity that I was missing.  Now
| everything works beautifully.  So the big question at this point is
| why is the typedef necessary?  I thought that typedef was *NEVER* 
| required.  Anybody know the answer?

Today's answer is "broken compiler." The following program, using no
typedef, compiles and runs on SunOS3, Xenix 2.3.1, Stellar (SysV),
Ultrix (BSD) and Convex (BSD).
________________________________________________________________

#include <stdio.h>

int t1(), t2(), (*ptr[])() = { t1, t2 };

main() {
  int n = 1;

  (*ptr[n])();
}

t1() { printf("T1\n"); }
t2() { printf("T2\n"); }
________________________________________________________________

It also works with the declarations separate, such as:
	int t1();
	int t2();
	int (*ptr[])() = { t1, t2 };

There is no need for the typedef, and I belive your compiler is faulty
if it does not work. 'Scuse, I mean "*DOES**NOT**WORK*".
	bill davidsen		(davidsen at crdos1.crd.GE.COM)
  {uunet | philabs}!crdgw1!crdos1!davidsen
"Stupidity, like virtue, is its own reward" -me



More information about the Comp.lang.c mailing list