Bock 基础知识

Blocks 是C语言的扩充功能,带有自动变量的匿名函数。
 
C 函数指针类型变量 vs Block 类型变量
 
C 函数指针类型变量 Block 类型变量
返回值类型 方法名 参数列表 表达式
int func(int count) 
{
     return count + 1;
}
^返回值类型 参数列表 表达式
^int (int count)
{
     return count+1;
}
声明函数指针类型变量
int (*funcptr)(int);
声明Block类型变量
int (^blk) (int);
将定义的函数地址赋值给函数指针类型变量
int (*funcptr)(int) = &func;
使用Block 语法将Block 赋值给Block类型变量
int (^blk) (int) = ^int (int count) {return count+1;};
 
完整的Block 语法和一般的C语言函数定义相比,仅有两点不同:
(1)没有函数名
(2)带有“^”
 
 
在函数参数和返回值中使用Block类型变量的记述比较
 
不是要typedef 声明Block 类型
使用typedef 声明Block 类型
typedef int (^blk_t) (int);
void func(int (^blk)(int))
{}
void func(blk_t blk)
{}
int (^func())(int)
{
     return ^int (int count) {return count+1;};
}
blk_t func()
{
     blk_t blk = ^int (int count) {return count+1;};
     return blk;
}
 
 
 
原文地址:https://www.cnblogs.com/shuleihen/p/4353947.html