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.
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
.
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.
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++)
精彩评论