Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How does typedef-ing a block works

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?

like image 388
user1559227 Avatar asked Mar 09 '13 12:03

user1559227


People also ask

What does typedef function do?

A typedef, or a function-type alias, helps to define pointers to executable code within memory. Simply put, a typedef can be used as a pointer that references a function.

What is the use of typedef in structures?

typedef is a predefined keyword. You can replace the name of the existing data type with the name which you have provided. This keyword helps in creating a user-defined name for an existing data type. You can also use the typedef keyword with structures, pointers, arrays etc.

What is the use of typedef in C++?

The typedef in C/C++ is a keyword used to assign alternative names to the existing datatypes. It is mostly used with user-defined datatypes when the naming of the predefined datatypes becomes slightly complicated to use in programs.

What is typedef example?

typedef struct { int scruples; int drams; int grains; } WEIGHT; The structure WEIGHT can then be used in the following declarations: WEIGHT chicken, cow, horse, whale; In the following example, the type of yds is "pointer to function with no parameter specified, returning int ".


1 Answers

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.

like image 60
Martin R Avatar answered Oct 06 '22 10:10

Martin R