POV-Ray : Newsgroups : povray.off-topic : Random C craziness : Re: Random C craziness Server Time
7 Sep 2024 05:13:53 EDT (-0400)
  Re: Random C craziness  
From: Vincent Le Chevalier
Date: 24 Jul 2008 08:24:57
Message: <48887499@news.povray.org>
John VanSickle a écrit :
> Invisible wrote:
>> Can somebody interpret this for me?
>>
>>   while (TRUE) { cont = (*cont)(); }
>>
>> What on earth...?
> 
> cont is a pointer to a function that is expected to return a pointer to 
> a function.  The code here causes a series of functions to be called, 
> each one specifying another function to be called afterwards by 
> returning the address of the function to be called.
> 

I was thinking the same, but I can't seem to find the right type 
definition to make it work without casting to and from void*... Which is 
apparently illegal 
(http://www.parashift.com/c++-faq-lite/pointers-to-members.html#faq-33.8)

Here's what I ended up with:

typedef void* (*pfunc)();

void* test1()
{
     return (void*)test1;
}

int main()
{
     pfunc cont = test1;
     while (true) { cont = (pfunc)(*cont)(); }
     return 0;
}

I'd like to write:

typedef pfunc (*pfunc)();

pfunc test1()
{
     return test1;
}

int main()
{
     pfunc cont = test1;
     while (true) { cont = (*cont)(); }
     return 0;
}

But the typedef here does not seem to work. I can't say I blame the 
compiler for choking on that one either :-)

I'm not too comfortable with function pointers though, so it's likely 
that I'm missing something here... Any hints?

-- 
Vincent


Post a reply to this message

Copyright 2003-2023 Persistence of Vision Raytracer Pty. Ltd.