Re: Pointers to functions: Is this the right [f03] syntax?
- From: glen herrmannsfeldt <gah@xxxxxxxxxxxxxxxx>
- Date: Thu, 22 Jun 2006 00:41:29 -0700
Richard Maine wrote:
(snip)
I think I'll let someone else comment on the legality of that. I'm not
sure. I would probably argue that it is invalid. I know that you can
have a pointer that can point to either a function or a subroutine, but
I think I'd argue that once you establish that it is one or the other in
a specific scope, that fact "sticks". After all - you for sure can't
have it be a function returning two different types in two different
references in the same scope; there's just no way to declare such a
dynamic change. Referencing the pointer as a function or subroutine
ought to establish adequately.
But if only one type of reference can be used, consider (using
a name passed as an argument, but it could work the same for a
pointer variable)
SUBROUTINE CALL(I,NAME,RET)
RET=0
IF(I.EQ.0) CALL NAME(RET)
IF(I.NE.0) RET=NAME(I)
RETURN
END
Now, it will never call both the subroutine and function during
a single call to CALL, but both references are in the same scope.
It should be possible to do something similar with procedure
pointers, maybe even allow for different function return types.
In C you would assign the function pointer to the appropriate
type of pointer, or just cast it. C doesn't have subroutines,
but functions returning void should be similar.
#include <stdio.h>
int main() {
int i,j;
double ret;
void one();
double two();
call(0,one,&ret);
printf("%f\n",ret);
call(3,two,&ret);
printf("%f\n",ret);
}
/* name is declared as a pointer to a function
returning int, but is cast to a pointer to a function
returning either void or double as needed */
int call(int i, int(*name)(),double *ret) {
if(i==0) ((void(*)())name)(ret);
else *ret=((double(*)())name)(i);
}
void one(double *ret) {
*ret=3.14;
}
double two(i) {
return i*i;
}
-- glen
.
- Follow-Ups:
- Re: Pointers to functions: Is this the right [f03] syntax?
- From: Richard Maine
- Re: Pointers to functions: Is this the right [f03] syntax?
- References:
- Pointers to functions: Is this the right [f03] syntax?
- From: Pere
- Re: Pointers to functions: Is this the right [f03] syntax?
- From: Richard E Maine
- Re: Pointers to functions: Is this the right [f03] syntax?
- From: Andy
- Re: Pointers to functions: Is this the right [f03] syntax?
- From: Richard E Maine
- Re: Pointers to functions: Is this the right [f03] syntax?
- From: Andy
- Re: Pointers to functions: Is this the right [f03] syntax?
- From: Richard Maine
- Pointers to functions: Is this the right [f03] syntax?
- Prev by Date: Read JPEG Open Source Module??
- Next by Date: Re: Read JPEG Open Source Module??
- Previous by thread: Re: Pointers to functions: Is this the right [f03] syntax?
- Next by thread: Re: Pointers to functions: Is this the right [f03] syntax?
- Index(es):
Relevant Pages
|