By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
424,962 Members | 1,998 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 424,962 IT Pros & Developers. It's quick & easy.

qustion about function pointer.

P: n/a
Hi all

My question is why the code CAN pass the compiler , and all the resoult is
right?

I means in my mind the reffoo(3,*test); should not pass compiler.
#include <stdio.h>

int test(int a)
{
printf("%d\n", a);
}

int reffoo(int val,int (*fp)(int))
{
fp(val);
}

int main()
{

reffoo(3,*test);

reffoo(4,&test);

reffoo(5,test);

return 0;

}

thank you very much!
your key9
Sep 21 '06 #1
Share this Question
Share on Google+
9 Replies


P: n/a
key9 wrote:
Hi all

My question is why the code CAN pass the compiler , and all the resoult is
right?
Applying the unary & or * to a function name yields a
pointer to that function.
I means in my mind the reffoo(3,*test); should not pass compiler.
How about reffoo(4,&*&*&test); ?
#include <stdio.h>

int test(int a)
{
printf("%d\n", a);
}

int reffoo(int val,int (*fp)(int))
{
fp(val);
}

int main()
{

reffoo(3,*test);

reffoo(4,&test);

reffoo(5,test);

return 0;

}

thank you very much!
your key9


--
Nils O. Selåsdal
www.utelsystems.com
Sep 21 '06 #2

P: n/a
"key9" <ia*****@126.comwrites:
My question is why the code CAN pass the compiler , and all the resoult is
right?

I means in my mind the reffoo(3,*test); should not pass compiler.
[where "test" is a function name]

You've run into an oddity of the C language. A function name
used in an expression is called a "function designator", and it
is treated as follows:

Except when it is the operand of the sizeof operator54) or
the unary & operator, a function designator with type
``function returning type'' is converted to an expression
that has type ``pointer to function returning type''.

In other words, a function name by itself is a function pointer;
a function name with & is not a function pointer, but the
address-of operator makes it into one; and applying * to a
function pointer yields a function, but it is then converted
right back to a pointer to function based on this paragraph.

So, if "f" is a function, then f, *f, and &f are all equivalent.
--
"I hope, some day, to learn to read.
It seems to be even harder than writing."
--Richard Heathfield
Sep 21 '06 #3

P: n/a
Ben Pfaff <bl*@cs.stanford.eduwrites:
"key9" <ia*****@126.comwrites:
>My question is why the code CAN pass the compiler , and all the resoult is
right?

I means in my mind the reffoo(3,*test); should not pass compiler.

[where "test" is a function name]

You've run into an oddity of the C language. A function name
used in an expression is called a "function designator", and it
is treated as follows:

Except when it is the operand of the sizeof operator54) or
the unary & operator, a function designator with type
``function returning type'' is converted to an expression
that has type ``pointer to function returning type''.
[snip]

It's interesting that this doesn't mention the function's arguments,
which are part of its type.

--
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.
Sep 21 '06 #4

P: n/a
On Thu, 21 Sep 2006 18:46:19 +0800, "key9" <ia*****@126.comwrote:
>Hi all

My question is why the code CAN pass the compiler , and all the resoult is
right?

I means in my mind the reffoo(3,*test); should not pass compiler.
#include <stdio.h>

int test(int a)
{
printf("%d\n", a);
Didn't your compiler complain that this function should return an int?
Maybe you need to change the warning level.
>}

int reffoo(int val,int (*fp)(int))
{
fp(val);
Again.
>}

int main()
{

reffoo(3,*test);

reffoo(4,&test);

reffoo(5,test);

return 0;

}

Remove del for email
Sep 22 '06 #5

P: n/a
Ben Pfaff <bl*@cs.stanford.eduwrote:
>
So, if "f" is a function, then f, *f, and &f are all equivalent.
Not to mention **f, ***f, ****f, etc. Every time you convert the
pointer to a function, the implicit conversion just changes it right
back again.

-Larry Jones

It's not denial. I'm just very selective about the reality I accept.
-- Calvin
Sep 22 '06 #6

P: n/a
Barry Schwarz wrote:
On Thu, 21 Sep 2006 18:46:19 +0800, "key9" <ia*****@126.comwrote:
Hi all

My question is why the code CAN pass the compiler, and all the resoult is
right?

I means in my mind the reffoo(3,*test); should not pass compiler.

#include <stdio.h>

int test(int a)
{
printf("%d\n", a);

Didn't your compiler complain that this function should return an int?
Maybe you need to change the warning level.
Maybe, but this isn't a constraint violation. Indeed, it's actually a
'feature'
of C that a non-void function is not required to return a value.
}

int reffoo(int val,int (*fp)(int))
{
fp(val);

Again.
Ditto.
}

int main()
{
reffoo(3,*test);
reffoo(4,&test);
reffoo(5,test);
Since the return value is not used, this code is well defined.
return 0;

}
--
Peter

Sep 22 '06 #7

P: n/a
Peter Nilsson wrote:
Barry Schwarz wrote:
>On Thu, 21 Sep 2006 18:46:19 +0800, "key9" <ia*****@126.comwrote:
>>Hi all

My question is why the code CAN pass the compiler, and all the resoult is
right?

I means in my mind the reffoo(3,*test); should not pass compiler.

#include <stdio.h>

int test(int a)
{
printf("%d\n", a);
Didn't your compiler complain that this function should return an int?
Maybe you need to change the warning level.

Maybe, but this isn't a constraint violation. Indeed, it's actually a
'feature'
of C that a non-void function is not required to return a value.
>>}

int reffoo(int val,int (*fp)(int))
{
fp(val);
Again.

Ditto.
>>}

int main()
{
reffoo(3,*test);
reffoo(4,&test);
reffoo(5,test);

Since the return value is not used, this code is well defined.
No, it is not. What gave you that idea ?
Sep 22 '06 #8

P: n/a
On 21 Sep 2006 22:05:53 -0700, "Peter Nilsson" <ai***@acay.com.au>
wrote:
>Barry Schwarz wrote:
>On Thu, 21 Sep 2006 18:46:19 +0800, "key9" <ia*****@126.comwrote:
>Hi all

My question is why the code CAN pass the compiler, and all the resoult is
right?

I means in my mind the reffoo(3,*test); should not pass compiler.

#include <stdio.h>

int test(int a)
{
printf("%d\n", a);

Didn't your compiler complain that this function should return an int?
Maybe you need to change the warning level.

Maybe, but this isn't a constraint violation. Indeed, it's actually a
'feature'
of C that a non-void function is not required to return a value.
I know this is true for main but is it also true for other functions?
Remove del for email
Sep 22 '06 #9

P: n/a
Barry Schwarz schrieb:
On 21 Sep 2006 22:05:53 -0700, "Peter Nilsson" <ai***@acay.com.au>
wrote:
>>Barry Schwarz wrote:
>>>On Thu, 21 Sep 2006 18:46:19 +0800, "key9" <ia*****@126.comwrote:

My question is why the code CAN pass the compiler, and all the resoult is
right?

I means in my mind the reffoo(3,*test); should not pass compiler.

#include <stdio.h>

int test(int a)
{
printf("%d\n", a);

Didn't your compiler complain that this function should return an int?
Maybe you need to change the warning level.

Maybe, but this isn't a constraint violation. Indeed, it's actually a
'feature'
of C that a non-void function is not required to return a value.

I know this is true for main but is it also true for other functions?
Yes. The behaviour becomes undefined if someone tries to use
the not returned value (C99, 6.9.1#12); in the case of main(),
the falling off the end often is equivalent to "return 0;" in
C89 implementations -- and C99 demands that (5.1.2.2.3#1).

Cheers
Michael
--
E-Mail: Mine is an /at/ gmx /dot/ de address.
Sep 23 '06 #10

This discussion thread is closed

Replies have been disabled for this discussion.