Relay-Version: version B 2.10 5/3/83; site utzoo.UUCP Path: utzoo!mnetor!seismo!mcnc!gatech!rutgers!sri-unix!husc6!cca!g-rh From: g-rh@cca.CCA.COM (Richard Harter) Newsgroups: comp.lang.c Subject: Another portable code question Message-ID: <16673@cca.CCA.COM> Date: Sat, 13-Jun-87 04:12:05 EDT Article-I.D.: cca.16673 Posted: Sat Jun 13 04:12:05 1987 Date-Received: Sat, 20-Jun-87 19:35:58 EDT Reply-To: g-rh@cca.UUCP (Richard Harter) Distribution: world Organization: Computer Corp. of America, Cambridge, MA Lines: 52 Here is one that has me puzzled. We have a C program which runs on a large variety of UNIX boxes (system V and bsd), VMS, and PRIMOS. One of the files has code that looks like this: extern int (*command_table[])(); ..... command_processor() { .... int (*command)(); .... command = command_table[n]; .... (*command)(args); .... } The declaration for command_table declares it as an array of pointers to functions returning int. (Confirmed by cdecl) The declar- ation for command declares it as a pointer to a function returning int. We pick off the appropriate command and execute it. Now, on a wide variety of UNIX systems the line "(*command)(args);" may be written as "(command)(args)". We have observed: (a) On most UNIX machines (VAX BSD and Sun in particular) either form is acceptable and will compile and execute correctly. Moreover lint will accept either form without complaint. (VMS C also takes either form without complaint or error.) (b) One vendor's compiler (I won't name it because I don't have the log sheets at hand) gives a compilation warning for (*command)(). (This is a UNIX box.) (c) The PRIMOS C compiler rejects (command)(args) and insists upon (*command)(args) First Question: Is the above code correct and legal C, or are we overlooking something subtle? Second Question: Why do both alternatives pass lint and execute correctly on most machines? Third Question: Are we correct in assuming that the compiler for vendor X is broken? Fourth Question: We are strongly committed to portable code and would like the code to be the same, in so far as possible, to be the same for all operating systems that we deal with. we have good reasons for using a jump table. What is the best way to code this with full portability? -- Richard Harter, SMDS Inc. [Disclaimers not permitted by company policy.]