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 pointers

Warning: Not directly related with Keil...


After the messages on the pointer and function pointer usage, I read the application note 129 and confused on the syntax of calling functions using funct. ptrs

I was using the syntax below:

void f()
{
}

void (* fptr)(void) = f;

...

void main()
{
   fptr();
}

This seems reasonable, because:
- fptr is a ptr
- if I can assign f to fptr, then f is also a ptr
- If I call f, which is a ptr, as "f()", then I should call fptr as "fptr()"

However, the note uses the below syntax:

   (*fptr)();

Which one is true?

I played with the code in the app. note and the compiler (v3.2) produced same output for both ways:

#pragma CODE DEBUG SYMBOLS LISTINCLUDE LARGE ROM(LARGE) 

#include <reg52.h>


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

int func(unsigned int count) reentrant
{
   long j;
   long k;

   k=0;
   for (j=0; j<count; j++)
      k+=j;

   return(k);
}


void main()
{
   func_caller(func);

   while(1);
}

And the code is:
   6          void func_caller(
   7             int (code *fptr)(unsigned int) reentrant)
   8          {
   9   1         unsigned char i;
  10   1         for (i=0; i<10; i++) {
  11   2            fptr(i);
  12   2         }
  13   1         for (i=0; i<10; i++) {
  14   2            (*fptr)(i);
  15   2         }
  16   1      }

                                           ; SOURCE LINE # 11
0015 7E00          MOV     R6,#00H
0017 900000  R     MOV     DPTR,#fptr
001A E0            MOVX    A,@DPTR
001B FC            MOV     R4,A
001C A3            INC     DPTR
001D E0            MOVX    A,@DPTR
001E FD            MOV     R5,A
001F 8982          MOV     DPL,R1
0021 8A83          MOV     DPH,R2
0023 120000  R     LCALL   ?C0015
0026 8002          SJMP    ?C0016
0028         ?C0015:
0028 E4            CLR     A
0029 73            JMP     @A+DPTR
002A         ?C0016:


                                           ; SOURCE LINE # 14
0041 7E00          MOV     R6,#00H
0043 900000  R     MOV     DPTR,#fptr
0046 E0            MOVX    A,@DPTR
0047 FC            MOV     R4,A
0048 A3            INC     DPTR
0049 E0            MOVX    A,@DPTR
004A FD            MOV     R5,A
004B 8982          MOV     DPL,R1
004D 8A83          MOV     DPH,R2
004F 120000  R     LCALL   ?C0017
0052 8002          SJMP    ?C0018
0054         ?C0017:
0054 E4            CLR     A
0055 73            JMP     @A+DPTR
0056         ?C0018:

0