qustion about function pointer.

K

key9

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
 
?

=?ISO-8859-1?Q?=22Nils_O=2E_Sel=E5sdal=22?=

key9 said:
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); ?
 
B

Ben Pfaff

key9 said:
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.
 
K

Keith Thompson

Ben Pfaff said:
key9 said:
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.
 
B

Barry Schwarz

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
 
L

lawrence.jones

Ben Pfaff said:
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
 
P

Peter Nilsson

Barry said:
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.

Since the return value is not used, this code is well defined.
 
?

=?ISO-8859-1?Q?=22Nils_O=2E_Sel=E5sdal=22?=

Peter said:
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.


Since the return value is not used, this code is well defined.

No, it is not. What gave you that idea ?
 
B

Barry Schwarz

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
 
M

Michael Mair

Barry said:
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
 

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
473,755
Messages
2,569,536
Members
45,007
Latest member
obedient dusk

Latest Threads

Top