2

I am using objective c to create a struct holding a variable length array. I know you can create an array of length n like so:

double array[n];

And i also believe in c++ you can declare:

vector<double> array;

where you do not have to declare the array length. Is there any way to do something similar in objective c? I am using ARC.

Thanks in advance, Ben

1 Answer 1

3

You just need to create an NSMutableArray like-

NSMutableArray *myArray = [NSMutableArray array];

[myArray addObject:....];// Add as many object as you want.

You just need to take care of one thing while creating variable length array, don't add nil as object, as nil is just to signify the end of the variable-length argument list.

EDIT - Might be following will help you - In this way you can define objective c data types in struct-

typedef struct{ 
    int numInputs; 
    __unsafe_unretained NSMutableArray *array;
} Pin;
Sign up to request clarification or add additional context in comments.

3 Comments

Thanks but you cannot have an NSMutableArray in a struct can you? - EDIT: im using ARC
typedef struct{ int numInputs; double vecWeight[99]; } Pin;
You can't have a variable length array in a struct. You can either use a custom class instead of a struct or use a pointer to a heap-allocated array in your struct. I.e. 'double* vecWeight;` and, later, struct Pin pin; pin.vecWeight = malloc(sizeof(double) * count);.

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.