typecasting towards a pointer to a K&R style declared function

D

dis

The following code introduces a 'generic function pointer' p. When calling
the pointed-to function via this p, one has to typecast p towards a pointer
to the type of the pointed-to function. My question is how to do this if the
pointed-to function is a K&R style declared function, like f. The best I
could come up with is a typecast towards a pointer to a function with
unspecified number and type of parameters (and appropriate return type), as
illustrated in the code below. Is this approach compliant with the C
standard?


f(a) float a;
{
return a;
}

main()
{
void (*p)() = (void(*)())f;
((int(*)())p)(2.3f);
return 0;
}
 
M

Mark McIntyre

The following code introduces a 'generic function pointer' p. When calling
the pointed-to function via this p, one has to typecast p towards a pointer
to the type of the pointed-to function. My question is how to do this if the
pointed-to function is a K&R style declared function, like f.

My question to you is, why do you care? K&R style C is now 15 years out of
date, and obsolete. If you are stuck with such a compiler, you have many
many more problems than this one.
 
E

Eric Sosman

dis said:
The following code introduces a 'generic function pointer' p. When calling
the pointed-to function via this p, one has to typecast p towards a pointer
to the type of the pointed-to function. My question is how to do this if the
pointed-to function is a K&R style declared function, like f. The best I
could come up with is a typecast towards a pointer to a function with
unspecified number and type of parameters (and appropriate return type), as
illustrated in the code below. Is this approach compliant with the C
standard?


f(a) float a;
{
return a;
}

main()
{
void (*p)() = (void(*)())f;
((int(*)())p)(2.3f);
return 0;
}

This looks right to me. Note that in a "K&R function"
a `float' argument is actually promoted to `double' at the
point of call and then demoted back to `float' by the time
the function's code gets to see it. Thus, the `2.3f' could
have been written as `2.3' with almost the same effect.
("Almost" because most computers use inexact approximations
for "two and three tenths," so `(double)2.3f != 2.3' might
be possible. When these potentially different `double'
values are demoted, it is possible that they might produce
different `float' values.)

Another consequence of the K&R rules is that the call
could also be written

((int(*)(double))p)(2.3f);

.... because f() actually receives a `double' value.

Mark McIntyre wonders why anyone would write f() in
the K&R style any more, and I think he's correct: the
newer "ANSI style" has too many advantages to ignore. If
you're writing new code that must call pre-ANSI functions
in an existing body of older code, though, this is about
the only reliable way to do it. However, it may be worth
while to "proto-ize" the existing code once and for all,
and do away with the problems. Question 11.31 in the
comp.lang.c Frequently Asked Questions (FAQ) list

http://www.eskimo.com/~scs/C-faq/top.html

.... lists some tools that can help automate this chore.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,143
Messages
2,570,822
Members
47,368
Latest member
michaelsmithh

Latest Threads

Top