1
int i;

va_list objects_list;
va_start(objects_list, objects);
for (id o = objects, i = 0; o != nil; o = va_arg(objects_list, id), i++);
objectsInArray = malloc(sizeof(id) * i);
va_end(objects_list);

// ... (malloc NULL checking is here, does not involve i)

va_start(objects_list, objects);
for (id o = objects, i = 0; o != nil; o = va_arg(objects_list, id), i++) {
  objectsInArray[i] = o;
}
va_end(objects_list);

I am getting an Array subscript is not an integer error on the objectsInArray[i] = o; line. objectsInArray is declared as id *objectsInArray.

i is an int, so why am I getting this error and how can I fix this? Thanks in advance.

3 Answers 3

4

i is of type id within the for loop. To resolve the ambiguous syntax, declare id o outside of the for(...) statement.

In Xcode, under project settings, enable the warnings for "Hidden local variables", so the compiler will warn for such things. Otherwise, when using gcc, use -Wshadow.

Sign up to request clarification or add additional context in comments.

2 Comments

@Radek: I added a suggestion to make the compiler warn about this type of bug.
@Radek: I don't. If you're using gcc, you can use -Wshadow to enable the same warnings.
2

No, you've created a new i which is of type id. Unfortunately, there is no way of doing "mixed-mode" initialisation in a for-loop.

Comments

1
for (id o = objects, i = 0; o != nil; o = va_arg(objects_list, id), i++)

includes the declaration

id o = objects, i = 0;

which means i is not an int, but an id. Declare o before the loop:

id o;
for (o = objects, i = 0; o != nil; o = va_arg(objects_list, id), i++)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.