views:

366

answers:

2

Hii all, m trying to make a 2d byte array . can any body give the code how to declare a NULL 2d byte array in Obj C for iPhone?

Thanks all.

+1  A: 

Since objective-c is a strict superset of c, you could just use a pure c definition and it would work fine:

char** myMatrix = malloc(width*height);

You could also use an NSArray of NSArrays, but that's not a 2 dimensional array. It's a jagged array and considerably less easy to use than a plain byte array.

Another alternative is using an NSData/NSMutableData object. That is the Foundation way of working with byte arrays. See NSMutableData class reference for more information.

NSMutableData* data = [NSMutableData dataWithLength:1024]; // One kilobyte
void* dataPointer = [data mutableBytes]; // Get a pointer to the raw bytes
Gerco Dries
Probably want to make that a char** myMatrix, so you can use the more normal notation of myMatrix[x][y]
Matthew Scharley
You are right, I added a *.
Gerco Dries
HI Greco!!!Can i replace that char* to byte*?
socialCircus
No, because byte is not a C type. The definition does not exist in the C language. Please also see the comment I added about NSData.
Gerco Dries
thanks A Lot greco but can i make a null data?
socialCircus
What do you mean by a null data? I assume you don't mean a null pointer of type char** (or void*) because that would be kind of silly. Perhaps we need a little more context in order to answer your question.
Gerco Dries
A: 

I'm cheating by doing this in C.

size_t width;
size_t height;
unsigned char *twoDimArray = calloc(width*height);
kbyrd
HI krbyrd!!! Can i replace that char* to byte*?
socialCircus
Neither the C or C++ standard has a 'byte' type. I don't know about Objective C.
kbyrd