Hello all,

I need to code a program where a function pointer '*fun1' is delivered as
input of function 'foo1'.
In standard C, 'foo1' is correct but foo2 gives the same result without
syntax error. I don't know whether 'foo2' is correct in C++.

Thank you for reading through it all and trying to help!

Gang-Gyoo JIN
-----------------------------------------

#include <stdio.h>
#include <math.h>

void foo1(float &x, float (*fun)(float x)){
x= x+(*fun)(x);
}

void foo2(float &x, float fun(float x)){
x= x+fun(x);
}

float fun1(float x){
return exp(-x);
}

void main(){
float x1= 0.0, x2= 0.0;

for(int j= 0; j < 10; j++) {
foo1(x1, fun1);
foo2(x2, fun1);
x1+= 0.1;
x2+= 0.1;
printf("%f %f\n",x1,x2);
}
}

Re: 'foo2' is correct in C++ by James

James
Thu Sep 30 00:44:13 CDT 2004

The body of foo2 is correct.
I'm not sure if the signature of foo2 is correct.
The signature of foo1 is offically the "correct" way if writing it,
though I think the signature of foo2 is allowed because what it's actually
saying is impossible.

ggjin wrote:
> Hello all,
>
> I need to code a program where a function pointer '*fun1' is
> delivered as input of function 'foo1'.
> In standard C, 'foo1' is correct but foo2 gives the same result
> without syntax error. I don't know whether 'foo2' is correct in C++.
>
> Thank you for reading through it all and trying to help!
>
> Gang-Gyoo JIN
> -----------------------------------------
>
> #include <stdio.h>
> #include <math.h>
>
> void foo1(float &x, float (*fun)(float x)){
> x= x+(*fun)(x);
> }
>
> void foo2(float &x, float fun(float x)){
> x= x+fun(x);
> }
>
> float fun1(float x){
> return exp(-x);
> }
>
> void main(){
> float x1= 0.0, x2= 0.0;
>
> for(int j= 0; j < 10; j++) {
> foo1(x1, fun1);
> foo2(x2, fun1);
> x1+= 0.1;
> x2+= 0.1;
> printf("%f %f\n",x1,x2);
> }
> }

--
Truth,
James Curran [MVP]
www.NJTheater.com (Professional)
www.NovelTheory.com (Personal)
MVP = Where your high-priced consultant goes for free answers





Re: 'foo2' is correct in C++ by Carl

Carl
Thu Sep 30 08:58:53 CDT 2004

James Curran wrote:
> The body of foo2 is correct.
> I'm not sure if the signature of foo2 is correct.
> The signature of foo1 is offically the "correct" way if writing it,
> though I think the signature of foo2 is allowed because what it's
> actually saying is impossible.

I believe foo1 and foo2 are both completely legal and their bodies
interchangeable in C or C++ (i.e. declare as float (*fun)(flot) and call as
fun(x) should also be legal).

Comeau agrees with me...

void foo1(float &x, float (*fun)(float x)){
x= x+(*fun)(x);
}

void foo2(float &x, float fun(float x)){
x= x+fun(x);
}

void foo3(float &x, float (*fun)(float x)){
x= x+fun(x);
}

void foo4(float &x, float fun(float x)){
x= x+(*fun)(x);
}

All of the above are legal and correct. Which is "best" is a matter of
style. I'd probably pick foo3, myself.

-cd



Re: 'foo2' is correct in C++ by Barry

Barry
Thu Sep 30 18:09:37 CDT 2004

On Thu, 30 Sep 2004 13:47:34 +0900, "ggjin"
<ggjin@hanara.kmaritime.ac.kr> wrote:

>Hello all,
>
>I need to code a program where a function pointer '*fun1' is delivered as
>input of function 'foo1'.
>In standard C, 'foo1' is correct but foo2 gives the same result without
>syntax error. I don't know whether 'foo2' is correct in C++.
>
>Thank you for reading through it all and trying to help!
>
>Gang-Gyoo JIN
>-----------------------------------------
>
>#include <stdio.h>
>#include <math.h>
>
>void foo1(float &x, float (*fun)(float x)){

This is not standard C.

> x= x+(*fun)(x);
>}
>
>void foo2(float &x, float fun(float x)){
> x= x+fun(x);
>}
>
>float fun1(float x){
> return exp(-x);
>}
>
>void main(){

This is non-standard in both C and C++.

> float x1= 0.0, x2= 0.0;
>
> for(int j= 0; j < 10; j++) {
> foo1(x1, fun1);
> foo2(x2, fun1);
> x1+= 0.1;
> x2+= 0.1;
> printf("%f %f\n",x1,x2);
> }
>}
>
The answer to your real question is that it is not necessary to use
the dereference operator (*) when attempting to invoke a function with
a properly initialized function pointer. Some time ago, the powers
that be decided that fun(...) and (*fun)(...) would have the same
meaning to the compiler. In fact (*****fun)(...) also has the same
meaning.


<<Remove the del for email>>

Re: 'foo2' is correct in C++ by Larry

Larry
Thu Sep 30 18:37:52 CDT 2004

email: donotspam_larry_brasfield@hotmail.com
Above views may belong only to me.
"Barry Schwarz" <schwarzb@deloz.net> wrote in message news:cji3nh$h5c$0$216.39.135.175@theriver.com...

[snip]
> The answer to your real question is that it is not necessary to use
> the dereference operator (*) when attempting to invoke a function with
> a properly initialized function pointer. Some time ago, the powers
> that be decided that fun(...) and (*fun)(...) would have the same
> meaning to the compiler. In fact (*****fun)(...) also has the same
> meaning.


So, how does one declare a pointer to a function pointer? Or,
if one can exist, how does one use it? Is it necessary to resort to
typedef in order to declare such a thing and use intermediate
variables in order to completely dereference it?

--
--Larry Brasfield



Re: 'foo2' is correct in C++ by Carl

Carl
Thu Sep 30 19:39:06 CDT 2004

Barry Schwarz wrote:
> On Thu, 30 Sep 2004 13:47:34 +0900, "ggjin"
> <ggjin@hanara.kmaritime.ac.kr> wrote:
>> void foo1(float &x, float (*fun)(float x)){
>
> This is not standard C.

The function pointer is standard C and C++. The reference argument is not C
(standard or otherwise).

> The answer to your real question is that it is not necessary to use
> the dereference operator (*) when attempting to invoke a function with
> a properly initialized function pointer. Some time ago, the powers
> that be decided that fun(...) and (*fun)(...) would have the same
> meaning to the compiler. In fact (*****fun)(...) also has the same
> meaning.

Not so.

fun(x) is converted to (*fun)(x) if fun has type "pointer to function", not
the other way around. (**fun)(x) means "call through a pointer to pointer
to function", etc for more *'s.

-cd



Re: 'foo2' is correct in C++ by Igor

Igor
Fri Oct 01 10:19:55 CDT 2004

"Larry Brasfield" <donotspam_larry_brasfield@hotmail.com> wrote in
message news:uLEj%23Z0pEHA.1816@TK2MSFTNGP09.phx.gbl
> email: donotspam_larry_brasfield@hotmail.com
> Above views may belong only to me.
> "Barry Schwarz" <schwarzb@deloz.net> wrote in message
> news:cji3nh$h5c$0$216.39.135.175@theriver.com...
>
> [snip]
>> The answer to your real question is that it is not necessary to use
>> the dereference operator (*) when attempting to invoke a function
>> with a properly initialized function pointer. Some time ago, the
>> powers that be decided that fun(...) and (*fun)(...) would have the
>> same meaning to the compiler. In fact (*****fun)(...) also has the
>> same meaning.
>
>
> So, how does one declare a pointer to a function pointer? Or,
> if one can exist, how does one use it? Is it necessary to resort to
> typedef in order to declare such a thing and use intermediate
> variables in order to completely dereference it?

Not at all. This works:

void f();

void (*pf)() = f;
pf(); // or (*pf)(); or (***pf)() and so on

void (**ppf)() = &pf;
(*ppf)(); // or (**ppf)() and so on


pf is a pointer to function. As such, pf is the same as *pf is the same
as **pf ...

ppf is a pointer to data. *ppf is a pointer to function. Thus *ppf is
the same as **ppf, and so on. But ppf is different from *ppf

In other words, * is only idempotent when applied to a function pointer.
If you have a pointer to a pointer to a function pointer, * behaves
normally (removes levels of indirection) until function pointer is
reached, and then becomes a no-op.
--
With best wishes,
Igor Tandetnik

"On two occasions, I have been asked [by members of Parliament], 'Pray,
Mr. Babbage, if you put into the machine wrong figures, will the right
answers come out?' I am not able to rightly apprehend the kind of
confusion of ideas that could provoke such a question." -- Charles
Babbage



Re: 'foo2' is correct in C++ by Igor

Igor
Fri Oct 01 10:34:17 CDT 2004

"Carl Daniel [VC++ MVP]"
<cpdaniel_remove_this_and_nospam@mvps.org.nospam> wrote in message
news:OegqR80pEHA.3876@TK2MSFTNGP15.phx.gbl
> Not so.
>
> fun(x) is converted to (*fun)(x) if fun has type "pointer to
> function", not the other way around. (**fun)(x) means "call through
> a pointer to pointer to function", etc for more *'s.

Not quite. Function call operator can be applied either to an lvalue of
function type, or to a function pointer (5.2.2/1). In the function calls
below, no conversions take place:

void f();
f();

void (*pf)() = f;
pf();
(*pf)(); // *pf is a function type, function call operator then applies
to it

Now consider (**pf)(). *pf is a function type ( void () ). It gets
implicitly converted to a pointer to function (4.3). Then * is appied,
and it becomes a function type again. A function call operator is then
used on it.

Similarly, with (*******pf)(), you simply have more iterations of
function-to-pointer conversion / dereference back to function sequence.
For the same reason, this works too: (*******f)() - no function pointers
necessary. For that matter, so does (**&*&**&f)() - you can have an
arbitrary combination of * and & as long as there are no two &'s in a
row.
--
With best wishes,
Igor Tandetnik

"On two occasions, I have been asked [by members of Parliament], 'Pray,
Mr. Babbage, if you put into the machine wrong figures, will the right
answers come out?' I am not able to rightly apprehend the kind of
confusion of ideas that could provoke such a question." -- Charles
Babbage