block中无法使用C数组变量

在Objective-C的block中无法使用C数组,即使我们不对C数组做任何改变,编译的时候也会报错:

#include <stdio.h>

int main() {
    const char text[] = "hello";  //声明数组类型变量
    void (^blk)(void) = ^{
        printf("%c\n", text[2]); //只是读取数组变量
    };
}

这是编译之后的结果:

 

解决的办法是使用指针声明数组:

#include <stdio.h>

int main() {
    const char *text = "hello";
    void (^blk)(void) = ^{
        printf("%c\n", text[2]);
    };
}

这样就能通过编译了。

 

posted @ 2016-05-05 08:11  chaoguo1234  阅读(212)  评论(0编辑  收藏  举报