views:

399

answers:

3

Hello,

I am programming in Objective-C but I would like to write a c function to increase the performance. I have written the code below this post but the compile keeps coming back with the following error:

error: expected specific-qualifier-list before 'bool'

error: expected '=', ',', ';', 'asm' or 'attribute' before 'addToBoolArray'

structs.h:

typedef struct boolArray{
bool *array;
int count;
} boolArray;

bool addToBoolArray(boolArray *bArray, bool newBool)

structs.c:

#import "structs.h"

bool addToBoolArray(boolArray *bArray, bool newBool)
{
if(bArray->count > 0){
 bArray->array = realloc(bArray->array,(bArray->count+1)*sizeof(bool));
else{
 bArray->array = (bool *)malloc(sizeof(bool));
}

if(bArray->array == NULL)
 return false;

bArray->array[bArray->count] = newBool;
bArray->count++;

return true;
}

I've found many forum threads about this error but none of them seem to address my issue. Any ideas?

Thank you

+3  A: 

There's no bool type in C89 or Objective-C.

For plain C89, typically int is used.

For C99, you can do:

#include <stdbool.h>

For Objective-C, it seems that there's a typedef for BOOL, and constants TRUE and FALSE, is NSObject.h.

Pavel Minaev
In C99 there's the predefined type `_Bool` which works with no need to #include any headers.
pmg
Yes, of course it's really just `typedef _Bool bool`. In practice, though, I haven't ever seen anyone use `_Bool` directly, just as noone uses `_Complex` directly. The sole reason why it was done that way is to avoid introducing new keywords that could clash with some that were already widely used.
Pavel Minaev
+1  A: 

You should probably use BOOL from <objc.h> , if you want to use (the c99 type) bool , incude <stdboolh>

You are also missing a ; after the declaration of addToBoolArray in your header file.

nos
+1  A: 

If you are trying to increase performance, you might want to use a bit vector instead of an array of bools…

Vincent Gable