Re: Pointers to functions: Is this the right [f03] syntax?



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

.



Relevant Pages

  • Re: confusion: casting function pointers
    ... pointer from the 'actual/other modules' that takes arguments of type ... list to types of void *). ... int main{ ... without a prototype, a number of special "promotion" rules take ...
    (comp.lang.c)
  • Re: invalid pointer adress
    ... >> pointer causes the program to exit with a core dump. ... struct s2{void *p;}; ... int leseExterneHinweise_masch_storno ... typedef struct s_AusdatFeldbeschreibung ...
    (comp.lang.c)
  • Should io(read|write)(8|16|32)_rep take (const|) volatile u(8|16|32) __iomem *addr?
    ... the destination pointer on user-space ... @src: ... const void *data, int bytelen); ...
    (Linux-Kernel)
  • Re: function pointer help!
    ... //the return void and input prameters are defined in the manual... ... void MyProjectView::CallHandler(int,unsigned int, unsigned int, void*) ... You are attempting to use a C++ member function as the callback, but the callback is defined in terms or C, not C++. ... The underlying problem is that C++ functions receive a hidden parameter, the 'this' pointer, so their signature is incompatible with C definitions. ...
    (microsoft.public.vc.mfc)
  • Re: Whats the meaning of this code
    ... void * work ... Whats the meaning of this line:- ... The meaning...a function called work that accepts a pointer of type ... pointer value is cast to an int value. ...
    (comp.lang.c)