3

I have a 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:

  1. 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);
    
  2. In the C file that includes this header, how can I handle these two arguments in the ClientAlloc function? Sample code will be great to me.

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

Thanks for your replies. By having two real functions, I pass those to the following code:

ClientAlloc(Enroll, Change)

However, when I compile the code, I got the following errors, is anything I missing here?

expected declaration specifiers or ‘...’ before ‘Enroll’
expected declaration specifiers or ‘...’ before ‘NotifyChange’
Jonathan Leffler
  • 730,956
  • 141
  • 904
  • 1,278
user3815726
  • 520
  • 2
  • 6
  • 20
  • 1
    @SouravGhosh, would you mind explaining a bit more? – user3815726 Jul 28 '15 at 07:15
  • In the first statement, you're creating an alias for `uint8_t`, in the second one, you're creating a function pointer. – Sourav Ghosh Jul 28 '15 at 07:16
  • For your update, we need to see those function definitions — or, at least, the signature part of the function definitions. Your code mentions 'Change' and the error message mentions 'NotifyChange'; that leads us to think there is some editing going on before you write to SO. If you're going to do that, make sure we can't spot that you're doing it! – Jonathan Leffler Jul 28 '15 at 07:36
  • @JonathanLeffler I'm not entirely sure this is a duplicate of that question... the second part of it seems like it might be asking something more... but okay. – autistic Jul 28 '15 at 07:36
  • @Freenode-newbostonSebivor: There's a wide range of possible alternative duplicates: [Understanding typedefs for function pointers](http://stackoverflow.com/questions/1591361/); [Typedeffing a function (NOT a function pointer)](http://stackoverflow.com/questions/2297064); [How do function pointers in C work](http://stackoverflow.com/questions/840501); and I'm sure there are other possibilities. – Jonathan Leffler Jul 28 '15 at 07:39

2 Answers2

1

No, they are not.

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

defines a function pointer type that matches with this signature. So,

uint8_t EnrollT(uint16_t test1, uint16_t test2);

matches with this signature and you can use 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 type EnrollT is "function with two uint16_t parameters returning uinit8_t. Also, you have,

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

So, if you have two functions matching EnrollT, ChangeT, you may call ClientAlloc passing theses two functions

Dr. Debasish Jana
  • 6,980
  • 4
  • 30
  • 69
0

Addressing the second question:

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

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

And then pass them to function call:

ClientAlloc(enroll, change);
Jonathan Leffler
  • 730,956
  • 141
  • 904
  • 1,278
gandgandi
  • 330
  • 1
  • 8