function declare

A

adream307

I want to declare a function like this:
(we named this type of function as F)
1. the return type of F is "void"
2. the parameter of F is a function pointer, this pointer point to a
function whose type is the same as F
can i declare a function like this
thank you
 
A

Alexander Bartolich

adream307 said:
I want to declare a function like this:
(we named this type of function as F)
1. the return type of F is "void"
2. the parameter of F is a function pointer, this pointer point to a
function whose type is the same as F
can i declare a function like this

No. Recursive declarations are not possible in C.
 
S

Shao Miller

I want to declare a function like this:
(we named this type of function as F)
1. the return type of F is "void"
2. the parameter of F is a function pointer, this pointer point to a
function whose type is the same as F
can i declare a function like this
thank you

Perhaps you could make use of an obsolescent C construct[6.11.6p1]:

#include <stdio.h>

typedef void f_unspec();
typedef void f_xxx(f_unspec *);

f_unspec foo;
f_xxx foo;

void foo(f_xxx * func) {
puts("foo()");
if (func == foo) {
puts("Called with self.");
return;
}
func(func);
return;
}

void bar(f_xxx * func) {
puts("bar()");
if (func == bar) {
puts("Called with self.");
return;
}
func(func);
return;
}

int main(void) {
foo(foo);
foo(bar);
bar(foo);
bar(bar);
return 0;
}
 
L

luser- -droog

Perhaps you could make use of an obsolescent C construct[6.11.6p1]:

This is a known aspect of the C type system which becomes known to anyonewho
understands
        mode m = proc(m)void;

This is similar to
        mode m = struct(ref m field);
In C
        typedef struct m{struct m *field;} m;

The difference is in Algol 68 the reach of a declaration starts from the
beginning of the scope rather than its first declaration. That allows recursive
and forward declarations such as

    begin
        proc yin = (S a)void: yang(y);
        proc yang = (P b)void: yin(x(x));
        P x, S y;
        mode P = proc(P)P;
        mode S = struct(ref S field);
        skip
    end

Wild!
:)
 
S

Shao Miller

Shao Miller<[email protected]> said:
On 5/29/2011 5:15 AM, adream307 wrote:
I want to declare a function like this:
(we named this type of function as F)
1. the return type of F is "void"
2. the parameter of F is a function pointer, this pointer point to a
function whose type is the same as F
can i declare a function like this
thank you
Perhaps you could make use of an obsolescent C construct[6.11.6p1]:

#include <stdio.h>

typedef void f_unspec();
typedef void f_xxx(f_unspec *);

f_unspec foo;
f_xxx foo;

void foo(f_xxx * func) {
puts("foo()");
if (func == foo) {
puts("Called with self.");
return;
}
func(func);
return;
}

void bar(f_xxx * func) {
puts("bar()");
if (func == bar) {
puts("Called with self.");
return;
}
func(func);
return;
}

int main(void) {
foo(foo);
foo(bar);
bar(foo);
bar(bar);
return 0;
}

This is a known aspect of the C type system which becomes known to anyone who
understands
mode m = proc(m)void;

This is similar to
mode m = struct(ref m field);
In C
typedef struct m{struct m *field;} m;

The difference is in Algol 68 the reach of a declaration starts from the
beginning of the scope rather than its first declaration. That allows recursive
and forward declarations such as

begin
proc yin = (S a)void: yang(y);
proc yang = (P b)void: yin(x(x));
P x, S y;
mode P = proc(P)P;
mode S = struct(ref S field);
skip
end

Wild!
:)

Though I'm not 100% sure what that ("the reach") has to do with the C
code example... Maybe it doesn't and that's why the example was not
present in the response from "China Blue Angels". :)
 

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,773
Messages
2,569,594
Members
45,122
Latest member
VinayKumarNevatia_
Top