[comp.lang.c] Initializers containing function names

MICPRF@latvax8.lat.oz (03/13/90)

Greetings net people,
	I don't know if anyone out there can help me but here is my
problem. In trying to install the S stats/graphics package on my
Sys V box I struck the following difficulty:
My cc compiler complains about illegal initialization if an attempt is
made to include a function name in an initializer. (The writer(s) of S
used this to set up a hash table of functions). According to the manuals
and references I have it should be perfectly OK. The function name should
be converted implicitly to a constant of type pointer to function and
should be able to used as an initializer. The following fragment illustrates
the problem:

int dummy(){};
int address = (int) dummy;

In the S code this type of thing is embedded in arrays of structures, for
the hash tables but the problem seems to come down to the above. Any hints
or suggestions for work arounds gratefully accepted. Perhaps others have
experienced similar problems? My machine is VME-bus M68010 based running
Sys V.2 (a Microforce 2, Force Computers, Munich).
				Paul Fisher,
				Microbiology Dept.,
				La Trobe University, Melbourne, Australia.
				(micprf@latvax8.lat.oz.au).

henry@utzoo.uucp (Henry Spencer) (03/14/90)

In article <383@latvax8.lat.oz> MICPRF@latvax8.lat.oz writes:
>My cc compiler complains about illegal initialization if an attempt is
>made to include a function name in an initializer...
>
>int dummy(){};
>int address = (int) dummy;
>
>In the S code this type of thing is embedded in arrays of structures, for
>the hash tables but the problem seems to come down to the above...

The problem may be the cast rather than the use of the function name.
Through what seems to have been an accidental oversight, casts were not
on the list of compile-time operators in K&R1, and some compilers have
faithfully perpetuated this mistake.

(Casting a function pointer to int is also a very machine-dependent
operation, which may not give meaningful results, but that's a different
issue.)
-- 
MSDOS, abbrev:  Maybe SomeDay |     Henry Spencer at U of Toronto Zoology
an Operating System.          | uunet!attcan!utzoo!henry henry@zoo.toronto.edu

MICPRF@latvax8.lat.oz (03/15/90)

In article <1990Mar13.174517.14634@utzoo.uucp>, henry@utzoo.uucp (Henry Spencer) writes:
> In article <383@latvax8.lat.oz> MICPRF@latvax8.lat.oz writes:
>>My cc compiler complains about illegal initialization if an attempt is
>>made to include a function name in an initializer...
>>
>>int dummy(){};
>>int address = (int) dummy;
>>
>>In the S code this type of thing is embedded in arrays of structures, for
>>the hash tables but the problem seems to come down to the above...
> 
> The problem may be the cast rather than the use of the function name.
> Through what seems to have been an accidental oversight, casts were not
> on the list of compile-time operators in K&R1, and some compilers have
> faithfully perpetuated this mistake.

Many thanks Henry. You got it in one! I wrote a couple of small test
routines and the compiler treats them in a manner completely consistent
with your suggestion. Trouble is I don't know how important it is to
S that the addresses for the functions in the hash tables be stored
in variables of type long. (I used int in my example for the heck of it
as on my machine int and long are both 32 bits and the behaviour was
the same). So I've replaced what was load time initialization with
a subroutine that assigns all the values at run time right at the start.

> 
> (Casting a function pointer to int is also a very machine-dependent
> operation, which may not give meaningful results, but that's a different
> issue.)

Yes you are right, I hadn't thought about this (see above).

> -- 
> MSDOS, abbrev:  Maybe SomeDay |     Henry Spencer at U of Toronto Zoology
> an Operating System.          | uunet!attcan!utzoo!henry henry@zoo.toronto.edu