Searching \ for '[PIC]: ANSI C question (complicated declaration. f' in subject line. ()
Make payments with PayPal - it's fast, free and secure! Help us get a faster server
FAQ page: www.piclist.com/techref/microchip/languages.htm?key=c
Search entire site for: 'ANSI C question (complicated declaration. f'.

Exact match. Not showing close matches.
PICList Thread
'[PIC]: ANSI C question (complicated declaration. f'
2002\09\12@083354 by Kari Lehikko

flavicon
face
Hi all,

Maybe someone of you wise people can help me... I find function pointers
very hard to understand :(

I need to declare a variable that holds a pointer to a function with no
(void) arguments and returns a pointer to a similar function. I've been
looking for an example all day long and found none.

if "fooptr" is this variable, I'd use it like this:

for(;;)
 fooptr=(*fooptr)();


TIA,

Kari Lehikko

--
http://www.piclist.com#nomail Going offline? Don't AutoReply us!
email spam_OUTlistservTakeThisOuTspammitvma.mit.edu with SET PICList DIGEST in the body


2002\09\12@090609 by cdb

flavicon
face
<HTML><HEAD>
</HEAD>
<BODY>
<div><FONT FACE="Arial" SIZE=3> Your not alone there are days when pointers just won't work for me!</FONT></div>
<div>&nbsp;</div>
<FONT FACE="Arial" SIZE=3> </FONT><div><FONT FACE="Arial" SIZE=3> However is the following what you mean the struct just happens to be some code I have written for a project.</FONT></div>
<div>&nbsp;</div>
<FONT FACE="Arial" SIZE=3> </FONT><div><b><FONT FACE="Arial" SIZE=3> Prototypes</FONT></b></div>
<div>&nbsp;</div>
<FONT FACE="Arial" SIZE=3> </FONT><div><FONT FACE="Arial" SIZE=3> struct Time</FONT></div>
<div><FONT FACE="Arial" SIZE=3> {</FONT></div>
<div><FONT FACE="Arial" SIZE=3> &nbsp;&nbsp; unsigned char Hours;</FONT></div>
<div><FONT FACE="Arial" SIZE=3> &nbsp;&nbsp; unsigned char Mins;</FONT></div>
<div><FONT FACE="Arial" SIZE=3> &nbsp;&nbsp; unsigned char Day;</FONT></div>
<div><FONT FACE="Arial" SIZE=3> &nbsp;&nbsp; unsigned char PeriodH;</FONT></div>
<div><FONT FACE="Arial" SIZE=3> &nbsp;&nbsp; unsigned char PeriodM;</FONT></div>
<div><FONT FACE="Arial" SIZE=3> };</FONT></div>
<div><FONT FACE="Arial" SIZE=3> &nbsp; Time Event[MAX_EV];</FONT></div>
<div><FONT FACE="Arial" SIZE=3> &nbsp; Time *pEvent;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </FONT></div>
<div>&nbsp;</div>
<FONT FACE="Arial" SIZE=3> </FONT><div>&nbsp;</div>
<FONT FACE="Arial" SIZE=3> </FONT><div><b><FONT FACE="Arial" SIZE=3> Function</FONT></b></div>
<div>&nbsp;</div>
<b><FONT FACE="Arial" SIZE=3> </FONT></b><div><FONT FACE="Arial" SIZE=3> void EventOnTime()</FONT></div>
<div><FONT FACE="Arial" SIZE=3>  {</FONT></div>
<div><FONT FACE="Arial" SIZE=3> &nbsp; </FONT></div>
<div><FONT FACE="Arial" SIZE=3> &nbsp; unsigned char ev;</FONT></div>
<div><FONT FACE="Arial" SIZE=3> &nbsp; </FONT></div>
<div><FONT FACE="Arial" SIZE=3> &nbsp; pEvent=Event;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; //Pointer to event structure.</FONT></div>
<div><FONT FACE="Arial" SIZE=3> &nbsp;&nbsp; </FONT></div>
<div><FONT FACE="Arial" SIZE=3> &nbsp;&nbsp; </FONT></div>
<div><FONT FACE="Arial" SIZE=3> &nbsp; for(ev=0;ev<MAX_EV;ev++)&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; //run through all the events</FONT></div>
<div><FONT FACE="Arial" SIZE=3> &nbsp;&nbsp; {</FONT></div>
<div><FONT FACE="Arial" SIZE=3> &nbsp;&nbsp;&nbsp;&nbsp; </FONT></div>
<div><FONT FACE="Arial" SIZE=3> &nbsp;&nbsp;&nbsp;&nbsp; if(min==pEvent->Mins && hour==pEvent->Hours && Dy==pEvent->Day)</FONT></div>
<div><FONT FACE="Arial" SIZE=3> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp; {</FONT></div>
<div><FONT FACE="Arial" SIZE=3> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; if(++z> MAX_EV)</FONT></div>
<div><FONT FACE="Arial" SIZE=3> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; {z=0;}&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; //z=index for next event to be </FONT><FONT FACE="Arial" SIZE=3> displayed</FONT></div>
<div><FONT FACE="Arial" SIZE=3> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; OUTPUT|=SOLENOID|LED;                //Switch Solenoid and ON LED</FONT></div>
<div><FONT FACE="Arial" SIZE=3> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; M_Diff=pEvent->Mins + pEvent->PeriodM;&nbsp; //add duration time </FONT><FONT FACE="Arial" SIZE=3> to start time</FONT></div>
<div><FONT FACE="Arial" SIZE=3> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; H_Diff=pEvent->Hours + pEvent->PeriodH;</FONT></div>
<div><FONT FACE="Arial" SIZE=3> &nbsp; </FONT></div>
<div><FONT FACE="Arial" SIZE=3> <snip>---------</FONT></div>
<div>&nbsp;</div>
<FONT FACE="Arial" SIZE=3> </FONT><div><FONT FACE="Arial" SIZE=3> If not then, well I've just made a fool of myself <grin></FONT></div>
<div>&nbsp;</div>
<FONT FACE="Arial" SIZE=3> </FONT><div><FONT FACE="Arial" SIZE=3> Colin</FONT></div>
<div><FONT FACE="Arial" SIZE=3> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </FONT></div>
<div><FONT FACE="Arial" SIZE=3> -- </FONT></div>
<div><FONT FACE="Arial" SIZE=3> cdb, .....bodgy1KILLspamspam@spam@optusnet.com.au on 12/09/2002</FONT></div>
</body></html>

--
http://www.piclist.com#nomail Going offline? Don't AutoReply us!
email listservspamKILLspammitvma.mit.edu with SET PICList DIGEST in the body


2002\09\12@092620 by Wagner R. Teixeira

flavicon
face
> Hi all,
>
> Maybe someone of you wise people can help me... I find function pointers
> very hard to understand :(

Yes, it really is for many people.

> I need to declare a variable that holds a pointer to a function with no
> (void) arguments and returns a pointer to a similar function. I've been
> looking for an example all day long and found none.

You cannot do that because it would result in a recursive declaration. I am
sure you can solve your problem using another simpler and much more
efficient solution.

Anyway, you can declare your function returning a 'void *' and securely cast
it to a function pointer:

===
#include <stdio.h>

#define FOREVER    while(1)

void *fn1( void );
void *fn2( void );

void *fn1( void )
{
       printf( "fn1\n" );
       return( ( void * )fn2 );
}

void *fn2( void )
{
       printf( "fn2\n" );
       return( ( void * )fn1 );
}

void main( void )
{
       void *footptr;

       footptr = fn1;
       FOREVER
               footptr = ( *( void *( * )( void ) )footptr)( );
}
===

> if "fooptr" is this variable, I'd use it like this:
>
> for(;;)
>   fooptr=(*fooptr)();

You can make a much clearer version:

===
#include <stdio.h>

#define FOREVER    while(1)

typedef void      *voidptr;
typedef voidptr ( *voidfnptr )( void );

voidptr fn1( void );
voidptr fn2( void );

voidptr fn1( void )
{
       printf( "fn1\n" );
       return( ( void * )fn2 );
}

voidptr fn2( void )
{
       printf( "fn2\n" );
       return( ( void * )fn1 );
}

void main( void )
{
       voidptr footptr;

       footptr = fn1; // dont forget to initialize the fn pointer...
       FOREVER
               footptr = ( ( voidfnptr )footptr )( );
}
===

> TIA,
>
> Kari Lehikko

WagnerT

---
Outgoing mail is certified Virus Free.
Checked by AVG anti-virus system (http://www.grisoft.com).
Version: 6.0.384 / Virus Database: 216 - Release Date: 21/08/02

--
http://www.piclist.com#nomail Going offline? Don't AutoReply us!
email .....listservKILLspamspam.....mitvma.mit.edu with SET PICList DIGEST in the body


2002\09\12@104825 by Byron A Jeff

face picon face
On Thu, Sep 12, 2002 at 03:25:33PM +0300, Kari Lehikko wrote:
> Hi all,
>
> Maybe someone of you wise people can help me... I find function pointers
> very hard to understand :(
>
> I need to declare a variable that holds a pointer to a function with no
> (void) arguments and returns a pointer to a similar function. I've been
> looking for an example all day long and found none.
>
> if "fooptr" is this variable, I'd use it like this:
>
> for(;;)
>   fooptr=(*fooptr)();

Well one thought comes to mind:

The UNIX signal interface uses such a definition. So let's see what it says:

void (*signal(int signum, void (*handler)(int)))(int)

And there's a bit more text:
----------------------
If you're confused by the prototype at  the  top  of  this
manpage, it may help to see it separated out thus:

typedef void (*sighandler_t)(int);
sighandler_t signal(int signum, sighandler_t handler);
----------------------

So it seems to me that if you ditch the parameters, then you'd have your
definition.

In this case the void pointer is your friend.

BAJ

--
http://www.piclist.com#nomail Going offline? Don't AutoReply us!
email EraseMElistservspam_OUTspamTakeThisOuTmitvma.mit.edu with SET PICList DIGEST in the body


2002\09\12@134956 by Bob Ammerman

picon face
to avoid the recursion problem while maintaining type safety, try this (code
tested with Borland C++ 5.01):

// declare a struct, but don't say what is in it yet

struct x;

// declare STATEFUNCPTR to be a pointer to a func returning an 'x'

typedef struct x (*STATEFUNCPTR)(void);

// Now explain what is in an 'x'
typedef struct x {
   STATEFUNCPTR ptr;
} x;

// predeclare your state functions

x func_a(void);
x func_b(void);

// one way of selecting the next function to run
x func_a(void)
{
   static x goto_a = {func_a};
   static x goto_b = {func_b};

   if (1==1)
       return goto_a;
   else
       return goto_b;
}

// another way of doing it
x func_b(void)
{
       x result;

       if  (1==1)
           result.ptr = func_a;
      else
        result.ptr = func_b;

     return result;
}

// Now the 'main' routine

void test(void)
{
    x foo_ptr = {func_a};
    for (;;)
        foo_ptr = (*foo_ptr.ptr)();
}



Bob Ammerman
RAm Systems

--
http://www.piclist.com#nomail Going offline? Don't AutoReply us!
email listservspamspam_OUTmitvma.mit.edu with SET PICList DIGEST in the body


2002\09\12@150451 by Wagner R. Teixeira

flavicon
face
Very creative, but I still recommend clearer solutions, unless the code will
never be maintained... Certainly you can solve the original demand planning
better code structure.

What about Kari post the problem?

WagnerT

{Quote hidden}

---
Outgoing mail is certified Virus Free.
Checked by AVG anti-virus system (http://www.grisoft.com).
Version: 6.0.384 / Virus Database: 216 - Release Date: 21/08/02

--
http://www.piclist.com#nomail Going offline? Don't AutoReply us!
email KILLspamlistservKILLspamspammitvma.mit.edu with SET PICList DIGEST in the body


2002\09\12@164332 by Peter L. Peres

picon face
Your program will run out of stack soon because you used recursion (tail
recursion).

Peter

--
http://www.piclist.com#nomail Going offline? Don't AutoReply us!
email RemoveMElistservTakeThisOuTspammitvma.mit.edu with SET PICList DIGEST in the body


2002\09\12@193021 by Bob Ammerman

picon face
I really think this technique is quite clear. Of course, in a "real"
situation I would use better (longer) names and lots more comments.

Bob Ammerman
RAm Systems

{Original Message removed}

More... (looser matching)
- Last day of these posts
- In 2002 , 2003 only
- Today
- New search...