views:

31

answers:

1

There is no practical reason for this, its just something I was looking at today. Does anyone know if there is a way to create a Block that does not take any arguments. In the example below Block_001 would return 101, does anyone know what I am missing?

int (^Block_001) = ^{ return 101; };

If I add a int argument then the block compiles just fine

int(^Block_001)(int) = ^(int arg){ return 101; };

Gary

+1  A: 

You need the void.

int (^Block_001)(void) = ^{ return 101; };
Carl Norum
Hi, thank you, so a block always takes an arg, you just need to declare it as void, ok. So you don't need to do anything with the block literal, I assume if its void you don't need to name it and just leave it out?
fuzzygoat
@fuzzygoat, I'm not really an expert. I just put your code in a file and did `cc -c file.c` and then took my best guess. The code I have there at least *compiles*, I didn't do any further testing.
Carl Norum
No problem Carl, much appreciated.
fuzzygoat
@fuzzygoat: Carl's code doesn't specify a block that takes one argument of type void. It's a C idiom to declare that a function that takes no arguments. It looks a little wonky, but that is the right way to do it. The syntax works that way for historical reasons that are frankly braindead.
Chuck
Hi Chuck, sorry my mistake, I did know that, but thank you for the clarification. Its like C with the Block name too, the first time I tried I had Block_001 where what I actually needed was Block_001()
fuzzygoat
@fuzzygoat, blocks and function pointers have pretty similar syntax.
Carl Norum