How does typedef-ing a block works

user1559227 picture user1559227 · Mar 9, 2013 · Viewed 30.3k times · Source

In C/Obj-C, we do a typedef like this typedef int MYINT; which is clear.

Doing typedef for a block -typedef void (^MyBlock) (int a);

Now, we can use MyBlock.

Shouldn't it be like - typedef void (^MyBlock) (int a) MyBlock; similar to #define?

How the syntax works?

Answer

Martin R picture Martin R · Mar 9, 2013

See Declaring a Block Reference in "Blocks Programming Topics":

Block variables hold references to blocks. You declare them using syntax similar to that you use to declare a pointer to a function, except that you use ^ instead of *.

So

 typedef void (^myBlock) (int a);

defines a the type of a block using the same syntax as

 typedef void (*myFunc) (int a);

declares a function pointer.

See e.g. Understanding typedefs for function pointers in C for more information about function pointers.