1

I am trying to make a 2D byte array.

Can anybody give the code how to declare a NULL 2D byte array in Objective-C?

2 Answers 2

3

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
Sign up to request clarification or add additional context in comments.

3 Comments

Probably want to make that a char** myMatrix, so you can use the more normal notation of myMatrix[x][y]
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.
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.
0

I'm cheating by doing this in C.

size_t width;
size_t height;
unsigned char *twoDimArray = calloc(width*height);

1 Comment

Neither the C or C++ standard has a 'byte' type. I don't know about Objective C.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.