C - the typedef function used as a pointer in the argument of another function

My header file defines some code shown below:

typedef uint8_t EnrollT(uint16_t test1, uint16_t test2);
typedef void ChangeT(uint64_t post1, uint8_t post2);

struct ClusterT * ClientAlloc(EnrollT *, ChangeT *);

      

I have the following questions:

  • Is the following code equal?

    typedef uint8_t EnrollT(uint16_t test1, uint16_t test2);
    typedef uint8_t (*EnrollT)(uint16_t test1, uint16_t test2);
    
          

  • In the C file that includes this header, how can I handle these two arguments in the ClientAlloc function? The sample code would be great for me.

=============================================== === =======================

Thank you for your responses. Having two valid functions, I pass them to the following code:

ClientAlloc(Enroll, Change)

      

However, when I compile the code, I got the following errors: is there something missing here?

expected declaration specifiers or ‘...’ before ‘Enroll’
expected declaration specifiers or ‘...’ before ‘NotifyChange’

      

+3


source to share


2 answers


No, it is not.

typedef uint8_t (*PEnrollT)(uint16_t test1, uint16_t test2);

      

defines the type of function pointer that matches this signature. So,

uint8_t EnrollT(uint16_t test1, uint16_t test2);

      

matches this signature and you can use it like: PEnrollT pfn = EnrollT; and use as pfn (....); // equivalent to calling EnrollT



Now you have

typedef uint8_t EnrollT(uint16_t test1, uint16_t test2);
typedef void ChangeT(uint64_t post1, uint8_t post2);

      

So, you have a type EnrollT, which is a function with two uint16_t parameters returning uinit8_t. Also, you have

struct ClusterT * ClientAlloc(EnrollT *, ChangeT *);

      

So if you have two functions corresponding to EnrollT, ChangeT, you can call ClientAlloc by passing two functions

+1


source


Solution to the second question:

You must create two functions with the signature defined by EnrollT and ChangeT (but you cannot use the EnrollT and ChangeT types by name):

uint8_t enroll(uint16_t test1, uint16_t test2){ ... };
void change(uint64_t post1, uint8_t post2){ ... };

      



And then pass them to the function call:

ClientAlloc(enroll, change);

      

0


source







All Articles