I've been reading about this for a while and I'm not sure I have found a good answer.
I'm trying to setup an array of 92 structs. It's a fixed length and will not change as it's effectively a lookup table. I thought that the best way to do this was to first allocate the memory with calloc
and then load the data.
But after some reading I see a lot of people allocating the memory directly without calloc
or malloc
like this
myStruct myData[92] = { {1,2}, {3,4}, ....};
My first question is whether it is better to dynamically allocate the memory? My understanding was that this was a better solution. Especially if the data is not necessarily going to be used all the time.
My second question is in regards to initialising the data. I had read that I can initialise a struct using ... = {....};
but the compiler is not accepting that.
Here is the code I have so far:
typedef struct {
int a;
int b;
} myStruct;
@implementation MyClass
static myStruct *myData;
-(id) init {
// ...
myData = (myStruct *) calloc(92, sizeof(myStruct));
myData[0] = {1,2}; // <=== Error ! Compiler says "Expected expression!"
// ...