This discussion has been locked.
You can no longer post new replies to this discussion. If you have a question you can start a new discussion

function pointer

hi all,

do you think i can work around function pointers as following:

extern void a(void);
extern void b(void);

void fptr(void *fp)
{
if (fp == a)
a();
if (fp == b)
b();
}

void main(void)
{
void *fp;
fp = (void *)a; //or b
fptr(fp);
}

Parents Reply Children
  • Read the section on the OVERLAY directive in the manual. This directive allows you to manually edit the call tree, so that you can specify that a and b are actually called by fptr(), and not by where they might be referenced (main(), in this example).

    Once you make this change, then the call tree analysis done by the compiler will properly take care of the parameter and auto variable assignment.

  • Thanks Andy and Drew,

    I know i can use the OVERLAY directive but that would require me to manual edit the call tree. I need a solution that can automatically take care of this problem

    Application Note 129:

    void func_caller (
    long (code *fptr) (unsigned int))
    {
    unsigned char i;
    for (i = 0; i < 10; i++)
    {
    (*fptr) (i);
    }
    }

    extern void func_caller (long (code *) (unsigned int));
    int func (unsigned int count)
    {
    long j;
    long k;
    k = 0;
    for (j = 0; j < count; j++)
    {
    k += j;
    }
    return (k);
    }
    void main (void)
    {
    func_caller (func);
    while (1) ;
    }



    If I change func_caller to:

    void func_caller (
    long (code *fptr) (unsigned int))
    {
    unsigned char i;
    for (i = 0; i < 10; i++)
    {
    //(*fptr) (i);
    if (fptr == func)
    func(i);
    /*
    //Do this for all possible value of fptr
    if (fptr == func1)
    func1(i);
    if (fptr == func2)
    func2(i);
    */
    }
    }

    Would this solve function pointer problem?


    Anh

  • If I change func_caller:...

    Even if it works, you are still manually changing something -- the code.

    If you manually change the linker directives, what is the difference, really?

    Are you thinking that changes in the linker directives disappear and that you have to manually edit them for each build? They don't.

  • "Would this solve function pointer problem?"

    Possibly, but in a very wasteful way, and no more maintainable than using the Linker directives.

    You are effectively just using the function pointer as an index - so why not just use a plain byte index instead?

    void func_caller ( unsigned char index )
    {
       unsigned char i;
       for (i = 0; i < 10; i++)
       {
          switch( index )
          {
             case 1: func1(i);
                     break;
             case 2: func2(i);
                     break;
             case 3: func3(i);
                     break;
             // etc
          }
       }
    }
    You could, of course, define an enum to make things clearer.

    BTW: note the use of the < pre > and < /pre > tags to retain the formatting - see the instructions when you make a post.

  • Function pointers is a wonderful means of achieveing certain things on processors with a suitable architecture.

    Here in the '51 community we often see someone trying to force the '51 to "behave".

    Darn it, the '51 is NOT a PC. Accept the challenges/limitations or stick with your PC.

    Function pointers is a nightmare in the '51 architecture, not in Keil C.

    Erik