(Objective-)C does not support assignment of one array to another, there are workarounds involving struct
as they are assignable but you don't need to go there.
If you are after a constant array to be used by instances you can just declare it as static
:
static const float stops[2][2] = { {27.3, 51.7}, {93.2, 42.24}};
The static
makes stops
accessible only to code within the same file. You can place the declaration between the @implementation
and @end
to at least visually associate it as belonging to a class.
The above is not suitable if you need a variable array, but does form part of a solution. Keep your instance variable:
@private
float stops[2][2];
This must be an array, not some pointer, as it must allocate the space for your floats. Next use the above declaration but give it a different name:
static const float _stops_init[2][2] = { {27.3, 51.7}, {93.2, 42.24}};
and then in your init
use the standard C function memcpy()
to copy the values in the memory occupied by _stops_init
into the memory occupied by stops
:
memcpy(stops, _stops_init, sizeof(_stops_init));
Here sizeof()
will return the total size in bytes of the memory used by your float array _stops_init
and memcpy()
copies those bytes over those associated with stops
– that is it implements array assignment which C doesn't directly support.
Using the static const
array rather than a local variable define in init()
as the source of your values saves re-creating the source on every call to init()
.
The above code doesn't do all the checks it should - at minimum an assert()
checking that the sizes of stops
and _stops_init
are the same is advisable.
HTH