2007/08/22

typedef, pointer to array, function pointer

Function pointer
int func(int, float*);
/* declare a function pointer which return type is int, and the type of parameters are int and float*. */
int (*fp) (int, float*); 
fp = &function();  // equal to  fp = function();
float fArray[5]={};
(*fp)(3, fArray);  // equal to fp(3, fArray);

Pointer to array
int a[5];
/* declare a pointer to array which type is int and has 5 elements. */
int (*pa)[5]; 
pa = &a;   // assignment

Example1
int* (*a[5])(int, char*);
an array of function pointer which has 5 elements{
    each element is a function pointer{
        return type: int*
        parameter type: int and char*
    }
}

Example2
void (*b[5])(void (*)());
an array of function pointer which has 5 elements{
    each element is a function pointer{
        return type: void
        parameter type: a function pointer{
            return type: void
            parameter type: none
    }
}

Example3
double(*)()(*pa)[9];
an pointer to the array of function pointer which has 9 elements{
    each element is  a function pointer{
            return type: double
            parameter type:none
    }
}

Using typedef to improve readable.
Example1
int* (*a[5])(int, char*);
equal to
typedef int* (*FP)(int, char*);   //function pointer
FP a[5];

Example2
void (*b[5])(void (*)());
equal to
typedef void (*FP1)();          //function pointer
typedef void (*FP2)(FP1);   //function pointer
FP2 b[5];

Example3
double(*)()(*pa)[9];
equal to
typedef double (*FP)();   //function pointer.
typedef FP (*AP)[9];         //pointer to array.
AP pa;

No comments:

Post a Comment