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 point

flowing is error,Please help me!!

#include <stdio.h>
#define NUMBER 2


int max(int a[])
{
int z;
if(a[0]>a[1]) z=a[0]; else z=a[1];
return(z);
}



int process(void (* fun)(void *p),void *arg)
{
int z;
z=(int *)(* fun)((int *)arg);
return(z);
}



main()
{
int a[NUMBER];
int i;
int d;
for(i=0;i++;i<NUMBER) a[i]=i;

d=process(max,a);
while(1);
}

Parents
  • Your code is based on an invalid assumption, and it contains various bugs.

    Generally speaking, it's almost always an error to cast a function pointer to any other function pointer type.

    On top of that, the casts you do apply are wrong, here:

    int z;
    z=(int *)(* fun)((int *)arg); 
    

    The cast to (int *) is seriously wrong. You have to cast "fun" back to the real signature before you're allowed to use it for calling max(), which will fix the return type in the same step:

    
    typedef int (*my_fp_type)(int *);
    
    int z;
    z=((my_fp_type)fun)((int *)arg); 
    

Reply
  • Your code is based on an invalid assumption, and it contains various bugs.

    Generally speaking, it's almost always an error to cast a function pointer to any other function pointer type.

    On top of that, the casts you do apply are wrong, here:

    int z;
    z=(int *)(* fun)((int *)arg); 
    

    The cast to (int *) is seriously wrong. You have to cast "fun" back to the real signature before you're allowed to use it for calling max(), which will fix the return type in the same step:

    
    typedef int (*my_fp_type)(int *);
    
    int z;
    z=((my_fp_type)fun)((int *)arg); 
    

Children
  • Having fixed these problems, be sure to search both the App Notes and the Knowledgebase - the limitations of the 8051 architecture put some serious constraints on the use of function pointers in C51.
    You will get some very obscure bugs if you don't carefully observe all the precautions...!

    Search this forum, too.