Searching \ for '[OT] PIC-C syntax question' 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/devices.htm?key=pic
Search entire site for: 'PIC-C syntax question'.

Exact match. Not showing close matches.
PICList Thread
'[OT] PIC-C syntax question'
2000\05\23@133303 by Andrew Kunz

flavicon
face
I want a table in ROM of pointers to entry points into void functions.  How do I
declare this?

For instance:

void func1 (void);
void func2 (void);
void func3 (void);

<declaration?> test[] = { &func1, &func2, &func3 };


for (i = 0; i < 3; i++)
    test[i] ();

I need this from time to time, and always lose the code that worked.

Thanks.

Andy

2000\05\23@142921 by Jim Hartmann

flavicon
face
Function pointers are declared:
    return_type (*function_name)(function parameters);

An array of function pointers:
    return_type (*function_array_name[array_size])(function parameters);

The functions are declared in the usual way.

To pre-initialize a table in RAM:
    return_type (*function_array_name[array_size])(function parameters)
= {func1, func2, func3...};

To put the table in ROM:
    return_type (* const function_array_name[array_size])(function
parameters) = {func1, func2, func3...};

To change the table:
    function_array_name[x] = a_new_function;

To call...
    (*function_array_name[x])(parameters...);

that was fun,
:-Jim

2000\05\23@144544 by Barry King

flavicon
face
Andy asked:
> I want a table in ROM of pointers to entry points into void functions.  How do I
> declare this?

Here's what I did in a state machine in HiTech C:

// -------------------------------------------------------------------
// StateFn is a constant array of function pointers which is used
//         to dispatch the State functions.  The functions take
//         no parms and return nothing.

void (* const StateFn[])() = {
                                State0,  // Index 0
                                State1,  // Index 1
                                State2,  // Index 2
                             };

// each function looks like:
void State0(){
...
}

// To invoke a function given the Index
// (UIState = 0..2 in this example):

(*StateFn[UIState])(); // call the state Function based on UIState
                   // index

2000\05\23@144929 by Andrew Kunz

flavicon
face
Thanks guys!

That works.

Andy

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