[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.c

function declare

adream307

5/29/2011 10:16:00 AM

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
7 Answers

Alexander Bartolich

5/29/2011 10:54:00 AM

0

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

No. Recursive declarations are not possible in C.

--
host -t mx moderators.isc.org

adream307

5/29/2011 11:58:00 AM

0


> No. Recursive declarations are not possible in C.
-------------------
Thank you

Shao Miller

5/29/2011 5:08:00 PM

0

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;
}

China Blue Veins

5/29/2011 5:17:00 PM

0

In article <irtr18$e0j$1@dont-email.me>, Shao Miller <sha0.miller@gmail.com>
wrote:

> 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]:

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

--
I remember finding out about you, | I survived XYZZY-Day.
Everyday my mind is all around you,| I'm whoever you want me to be.
Looking out from my lonely room |Annoying Usenet one post at a time.
Day after day. | At least I can stay in character.

luserXtrog

5/30/2011 4:53:00 AM

0

On May 29, 12:16 pm, China Blue Angels <chine.b...@yahoo.com> wrote:
> In article <irtr18$e0...@dont-email.me>, Shao Miller <sha0.mil...@gmail.com>
> wrote:
>
> > 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]:
>
> 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!
:)

Shao Miller

5/30/2011 7:09:00 AM

0

On 5/29/2011 11:53 PM, luser- -droog wrote:
> On May 29, 12:16 pm, China Blue Angels<chine.b...@yahoo.com> wrote:
>> In article<irtr18$e0...@dont-email.me>, Shao Miller<sha0.mil...@gmail.com>
>> wrote:
>>
>>> 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". :)

Peter Nilsson

5/30/2011 11:25:00 PM

0

On May 29, 8:15 pm, adream307 <adream...@gmail.com> 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

<http://c-faq.com/decl/recurfunc...

--
Peter