Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use a c-type array as an ivar?

I need a good old-fashioned 2-dimensional array of integers in my program, but no matter how I try to declare it as an ivar, and then use @property/@synthesize, I get one compiler complaint or another.

I declare

int spotLocations[10] [10] 

as an ivar.

That much works, but then the @property/@synthesize process never passes muster.

like image 397
John R Doner Avatar asked Jan 29 '26 02:01

John R Doner


1 Answers

You can't do this. Array variabless can never be lvalues in C, which means you can never declare a function that returns an array, because it would be impossible to assign the result of the function to an array variable (since it can't be an lvalue).

Properties are just a shorthand way of declaring a function that returns a type. Since functions can never return arrays, you can never declare a property that is an array.

If you absolutely need to move matrices around like this, you could wrap it in a struct, which can be lvalues:

typedef struct {
  int value[10][10];
} matrix;

...

@property matrix spotLocations;

Of course, accessing the locations is a little more convoluted, you have to use

spotLocations.value[x][y]
like image 71
Rudedog Avatar answered Jan 31 '26 21:01

Rudedog



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!