473,394 Members | 1,965 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,394 software developers and data experts.

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

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;
}
Nov 14 '05 #1
2 2158
On Fri, 11 Jun 2004 17:17:50 +0200, in comp.lang.c , "dis"
<di*@hotmail.com> wrote:
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.
--
Mark McIntyre
CLC FAQ <http://www.eskimo.com/~scs/C-faq/top.html>
CLC readme: <http://www.angelfire.com/ms3/bchambless0/welcome_to_clc.html>
----== Posted via Newsfeed.Com - Unlimited-Uncensored-Secure Usenet News==----
http://www.newsfeed.com The #1 Newsgroup Service in the World! >100,000 Newsgroups
---= 19 East/West-Coast Specialized Servers - Total Privacy via Encryption =---
----== Posted via Newsfeed.Com - Unlimited-Uncensored-Secure Usenet News==----
http://www.newsfeed.com The #1 Newsgroup Service in the World! >100,000 Newsgroups
---= 19 East/West-Coast Specialized Servers - Total Privacy via Encryption =---
Nov 14 '05 #2
dis wrote:
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.

--
Er*********@sun.com

Nov 14 '05 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: Kapil Khosla | last post by:
Hi, I have been trying to understand this concept for quite sometime now somehow I am missing some vital point. I am new to Object Oriented Programming so maybe thats the reason. I want to...
5
by: Nimmi Srivastav | last post by:
When I was learning C, I learned that the data type of a function name by itself is a function pointer. For example int someFunction(char* str) { .... }
9
by: PapaTodd | last post by:
Greetings all, I've been fighting with this issue all day. This code is tried and true on other OS's, but when I compile and test on AIX 5.3 using xlC_r, I have problems. Here is the code...
11
by: Vinod Patel | last post by:
I have a piece of code : - void *data; ...... /* data initialized */ ...... struct known_struct *var = (struct known_struct*) data; /*typecasting*/ How is this different from simple...
13
by: brian | last post by:
Quick question: if I have a structure: struct foo { unsigned char *packet; unsigned char *ip_src; };
16
by: Abhishek | last post by:
why do I see that in most C programs, pointers in functions are accepted as: int func(int i,(void *)p) where p is a pointer or an address which is passed from the place where it is called. what...
5
by: WittyGuy | last post by:
How to typecast a "function pointer" to "const void*" type in C++ way? int MyFunction (double money); // Function prototype const void* arg = (const void*)MyFunction; // type casting...
12
by: bwaichu | last post by:
What is the best way to handle this warning: warning: cast from pointer to integer of different size I am casting in and out of a function that requires a pointer type. I am casting an...
26
by: Nishu | last post by:
Hi All, Is it valid in C to typecast a pointer? eg. code snippet... considering int as 16 bit and long as 32 bit. int *variable, value; *((long*)variable)++ = value; *((long*)variable)++...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.