1

How to declare a C array of integers as a property on an objective-c class?

5

1 Answer 1

6
@property (nonatomic, assign) int *array;
...
// somewhere in your code 
    int *gg = malloc(10 * sizeof(int));
    gg[1] = 1;
    gg[0] = 2;
    self.array = gg;

UPDATE:

This is heap based array now to make sure it will not be deallocated.
But don't forget to free it in dealloc free(self.array)

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

5 Comments

exactly what i was trying to achieve. the cleanest answer from all in other similar questions
great :)@NicolasManzini
You can't do this. When you leave the scope where gg was declared your array property will contain pointer to a deallocated part of memory. Best case scenario you will get trash instead of values you want. In case of int *array array must be allocated on heap using malloc or new.
the funny thing is I was reading about this heap/stack problem right now! :) i can fill it with 0s by doing gg[] = {0} ?
yep :) thanks to @creker for reminding :)

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.