4

I have a C array of blocks defined like this:

void (^block1)() = ^void() {
  doSomething1();
  doSomething2();
};

void (^block2)() = ^void() {
  doSomething3();
  doSomething4();
};

// etc.
typedef void (^myBlockType)();
myBlockType arrayBlock[2];
arrayBlock[0] = block1;
arrayBlock[1] = block2;

but then I am trying to access that C array from inside a block, something like

void (^adjust)(int value) = ^void(int value) {
    myBlockType myBlock = arrayBlock[value];
};

and Xcode is whining with

Cannot refer to declaration with an array type inside block

If this was an array of stuff like floats, integers, etc., there is this solution but how to do that for a C array of blocks?

Can I do

    myBlockType array = calloc(2, sizeof(myBlockType));

? Is this the correct approach? In that case how do I initialize the values?

Duck
  • 34,902
  • 47
  • 248
  • 470
  • Two questions (a) you don't say why you are not using an `NSArray`, that would be helpful to know (b) the linked *question* (not the *answer*) provides you with a solution - it has an array of X, you have an array of Y, makes no difference to procedure - so what about that didn't work for you? Might be best if you edit the question to answer (just leave a comment you've done so). – CRD Jul 27 '17 at 18:56
  • Sorry, my C sucks but I have to use a C array because the module I have to interact with, not written by me, is all written in C. Beyond the fact that my C sucks, my other problem is that that this other array is not accessible by using the syntax `array[i]`. I have tried it and Xcode whined all over the place. I guess that elements on that array has to be accessed using pointers and I don't have a clue on how to do that, or how even to fill that array with elements to start. – Duck Jul 27 '17 at 20:06
  • continued... and looking at the link with my poor knowledge of C does not make me see an answer there... if you can answer how I create that, populate and obtain the items, I will accept the answer... and yes, I know, my C stinks. Thanks – Duck Jul 27 '17 at 20:12
  • Seriously you need to learn some (more - Objective-C is a superset so you must know some) C if you are doing this. "my other problem is that that this other array is not accessible by using the syntax `array[i]`" - if you are importing a C array *or* pointer then this syntax *will work*. If "Xcode whined all over the place" then something is **wrong**. Figure out what (asking a question on SO if needed) before trying to move on. – CRD Jul 27 '17 at 20:23
  • this is why I have created this question. Lets hope someone knows how to do that. Thanks anyway. – Duck Jul 27 '17 at 20:26
  • This question is not about that "other array" and Xcode whining. Solve that issue *before* this one is what I'm recommending. – CRD Jul 27 '17 at 20:29

1 Answers1

0

OK, some pointers after the comment trail, but you need to learn and understand what is going on here.

Can I do

myBlockType array = calloc(2, sizeof(myBlockType));
  • In C arrays are pointers are closely interrelated. In the C array T x[n] the name x has type T * - a pointer. The index operation x[n] is defined to be the same as *(x + n) using pointer operations (* & +). So you can treat an array like a pointer and a pointer like an array, e.g. you can "index" a pointer.
  • As you have discovered you cannot capture a local array variable in a block.
  • You can capture a pointer-valued variable, but it is your responsibility to make sure what is pointed at remains valid.
  • Your calloc idea is one possible solution, but the types are wrong, array needs to be declared as a pointer.
  • Furthermore if you are using ARC (more than likely) and storing pointers of retainable object type, e.g. references to objects or blocks, then the type of array must include an ownership qualifier. You probably should use __strong so your "array" owns the items it references just as an NSArray does.
  • For anything you calloc (or malloc et al) you are responsible for calling free if and when required.

Understand those bullets and your possible solutions (there is more than one) just fall out.

HTH

Community
  • 1
  • 1
CRD
  • 52,522
  • 5
  • 70
  • 86