Re: multidimensional arrays
Re: multidimensional arrays
- Subject: Re: multidimensional arrays
- From: Andrew White <email@hidden>
- Date: Tue, 15 Mar 2005 10:15:04 +1100
Serge Meynard wrote:
You do realize that C fully supports multi-dimensional arrays, you can
do myArray[x][y] in plain C (and Objective-C is super set of C).
Only if your array size is fixed at compile-time. I'm talking about
arrays whose size is only known at execution time, and are allocated
dynamically.
In C, only the first dimension can be of unbounded size. The type and size
of the elements of a C array must be known at compile time, and that
includes lesser dimensions of an array of arrays.
If you are talking about a MyArray class in C++, that allows myArray[x][y]
access to arbitrary data, you are not talking C arrays. Rather, you are
talking syntactic sugar via operator overloading to an array that contains
array pointers. The underlying operation is still myArray->get (x,y).
You can do the equivalent in Objective-C:
@interface My2DArray : NSObject
{
NSArray * mArray;
}
- (id) objectAtIndex: (unsigned int) x index: (unsigned int) y;
@end
@implementation My2DArray : NSObject
- (id) objectAtIndex: (unsigned int) x index: (unsigned int) y
{
return
[((NSArray *) [objectAtIndex: x]) objectAtIndex: y];
}
@end
Add other messages to suit.
Note that NSArray is not semantically equivalent to a C array.
C array:
- statically typed to any single _statically sized_ type
NSArray
- dynamically sized
- Holds non-nil NSObject, including subclasses
- Implements retain / release semantics on entries
Implementing a class that allows more C-array like semantics isn't hard,
but you either need to use NSDictionary or do your own memory management.
--
Andrew White
--------------------------------------------------------------------------
This email and any attachments are confidential. They may contain legally
privileged information or copyright material. You should not read, copy,
use or disclose them without authorisation. If you are not an intended
recipient, please contact us at once by return email and then delete both
messages. We do not accept liability in connection with computer virus,
data corruption, delay, interruption, unauthorised access or unauthorised
amendment. This notice should not be removed.
_______________________________________________
Do not post admin requests to the list. They will be ignored.
Cocoa-dev mailing list (email@hidden)
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden