473,398 Members | 2,125 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,398 software developers and data experts.

function pointer question

MK
Hello,
I have been trying to get a handle on the following piece of code.
----- BEGIN ----
#include <stdio.h>

int
f2args(int a,int b)
{
printf("a=%d,b=%d\n",a,b);
return 1;
}

typedef int (*func3args)(int,int,int);

int
main(int argc,char **argv)
{
func3args fp = (func3args)f;
fp(1,2,3);
exit(0);
}
----- END ----

weird things:
- I get no compiler warnings or errors (porbably because I used a cast
(func)).
- And when I run I expect the program to crash, but it runs fine.

$ ./a.out
a=1,b=2

My questions are:
- Is this portable and guranteed by the C standard?
- Or is it the manifestation of the C calling convention (the
default), hence not portable or
implementation dependent.
I dug aroung and saw a lot of information regarding 'function pointer
compatibility', I guess I
don't fully understand that.

Thanks
MK

Mar 23 '06 #1
7 2087
I don't even see how that program would compile. Do you mean
'func3args fp = (func3args)f2args'?

If you did, you should get a compiler error complaining about too many
arguements.

Mar 23 '06 #2
MK
raxip wrote:
I don't even see how that program would compile. Do you mean
'func3args fp = (func3args)f2args'?

If you did, you should get a compiler error complaining about too many
arguements.


raxip:

I am sorry something went awry with the cut and paste yes it was, here
is the whole
program and compile and run log (Linux x86, gcc 3.3.5).
--- Begin xx.c ----
$ cat xx.c
#include <stdio.h>

int
func2args(int a,int b)
{
printf("a=%d,b=%d\n",a,b);
return 1;
}

typedef int (*func3args)(int,int,int);

int
main(int argc,char **argv)
{
func3args fp = (func3args)func2args;
fp(1,2,3);
exit(0);
}
------------------ End xx.c -------
$ cc xx.c

$ ./a.out
a=1,b=2

--------------------------
Now, I understand that casting func2args probably is 'shutting' the
compiler up. As I believe it should be legal to do that.

Mar 23 '06 #3
"MK" <Wa********@gmail.com> writes:
Hello,
I have been trying to get a handle on the following piece of code.
----- BEGIN ----
#include <stdio.h>

int
f2args(int a,int b)
{
printf("a=%d,b=%d\n",a,b);
return 1;
}

typedef int (*func3args)(int,int,int);

int
main(int argc,char **argv)
{
func3args fp = (func3args)f;
fp(1,2,3);
exit(0);
}
----- END ----

weird things:
- I get no compiler warnings or errors (porbably because I used a cast
(func)).
When I compiled it, I got:

tmp.c: In function `main':
tmp.c:15: error: `f' undeclared (first use in this function)
tmp.c:15: error: (Each undeclared identifier is reported only once
tmp.c:15: error: for each function it appears in.)

I presume your line
func3args fp = (func3args)f;
should be
func3args fp = (func3args)f2args;

If you're going to post code, it's very important to post the *exact*
code that you compiled. Copy-and-paste it, don't re-type it.
- And when I run I expect the program to crash, but it runs fine. $ ./a.out
a=1,b=2

My questions are:
- Is this portable and guranteed by the C standard?
- Or is it the manifestation of the C calling convention (the
default), hence not portable or
implementation dependent.
I dug aroung and saw a lot of information regarding 'function pointer
compatibility', I guess I
don't fully understand that.


The conversion is ok, but the call invokes undefined behavior.

C99 6.3.2.3p8:

A pointer to a function of one type may be converted to a pointer
to a function of another type and back again; the result shall
compare equal to the original pointer. If a converted pointer is
used to call a function whose type is not compatible with the
pointed-to type, the behavior is undefined.

--
Keith Thompson (The_Other_Keith) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.
Mar 23 '06 #4
MK
Keith,
Thanks for the reply. I did repost the correct code in my reply to
'raxip' post. I apologize. I see from your post that the behavior is
undefined. So what defines the functions 'type'? Is it the just
the 'return type' or the whole signature (including the
parameters?).
The reason I am interested in this, I am looking at a large
code-base, and this 'magic' seems to have been used a lot.
Thanks.

Mar 23 '06 #5
MK wrote:

My questions are:
- Is this portable and guranteed by the C standard?
- Or is it the manifestation of the C calling convention (the
default), hence not portable or
implementation dependent.
I dug aroung and saw a lot of information regarding 'function pointer
compatibility', I guess I
don't fully understand that.

Based on your second post, I'd say it's undefined behaviour which just
happens to wok. Correct me if I'm wrong (I silly thing to say here!),
but I don't think there is a standard calling convention.

A classic case of an evil cast.

--
Ian Collins.
Mar 23 '06 #6
"MK" <Wa********@gmail.com> writes:
Keith,
Thanks for the reply. I did repost the correct code in my reply to
'raxip' post. I apologize. I see from your post that the behavior is
undefined. So what defines the functions 'type'? Is it the just
the 'return type' or the whole signature (including the
parameters?).
The reason I am interested in this, I am looking at a large
code-base, and this 'magic' seems to have been used a lot.


The type of a function is determined by the return type and
parameters.

It's possible that your system's calling convention (something not
determined by the C language) is such that this kind of thing happens
to work. You *might* be better off leaving the code as it is,
undefined behavior and all, than fixing it at the risk of introducing
new bugs. You'll have to make that judgement yourself.

And please read <http://cfaj.freeshell.org/google/>.

--
Keith Thompson (The_Other_Keith) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.
Mar 23 '06 #7

"MK" <Wa********@gmail.com> wrote in message
news:11**********************@u72g2000cwu.googlegr oups.com...
Keith,
Thanks for the reply. I did repost the correct code in my reply to
'raxip' post. I apologize. I see from your post that the behavior is
undefined. So what defines the functions 'type'? Is it the just
the 'return type' or the whole signature (including the
parameters?).
The reason I am interested in this, I am looking at a large
code-base, and this 'magic' seems to have been used a lot.
Thanks.


Those casts make me think of a few things:

1) A couple of functions with a different number of arguments were merged
together
2) The underlying functions are written in another language, i.e., C
language with Pascal OS calls

The second is common on a number of miniframes.
Rod Pemberton
Mar 23 '06 #8

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

Similar topics

37
by: Ben | last post by:
Hi, there. Recently I was working on a problem where we want to save generic closures in a data structure (a vector). The closure should work for any data type and any method with pre-defined...
3
by: Bryan Parkoff | last post by:
Do C/C++ Compiler allow function to contain more than 8 parameters? I checked MS Visual C++ 6.0 that it can only limit 8 parameters, but most C/C++ Compiler can limit maximum 256 parameters. Can...
89
by: Sweety | last post by:
hi, Is main function address is 657. its show in all compiler. try it & say why? bye,
4
by: anonymous | last post by:
Thanks your reply. The article I read is from www.hakin9.org/en/attachments/stackoverflow_en.pdf. And you're right. I don't know it very clearly. And that's why I want to understand it; for it's...
41
by: Alexei A. Frounze | last post by:
Seems like, to make sure that a pointer doesn't point to an object/function, NULL (or simply 0) is good enough for both kind of pointers, data pointers and function pointers as per 6.3.2.3: 3 An...
3
by: Beta What | last post by:
Hello, I have a question about casting a function pointer. Say I want to make a generic module (say some ADT implementation) that requires a function pointer from the 'actual/other modules'...
54
by: John | last post by:
Is the following program print the address of the function? void hello() { printf("hello\n"); } void main() { printf("hello function=%d\n", hello); }
12
by: Googy | last post by:
Hi!! Can any one explain me the meaning of following notations clearly : 1. typedef char(*(*frpapfrc()))(); frpapfrc f; 2. typedef int (*(arr2d_ptr)()); arr2d_ptr p; 3. typedef int...
5
by: pauldepstein | last post by:
Hi all, I saw some code like this: unsigned short SomeFunc(unsigned short SomeNum, bool SomeBool, const SomeClass& SomeMem, bool(SomeClass::*AmemberFunctionOfSomeClass)(const unsigned...
20
by: MikeC | last post by:
Folks, I've been playing with C programs for 25 years (not professionally - self-taught), and although I've used function pointers before, I've never got my head around them enough to be able to...
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: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
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
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
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...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

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.