4

I have a function that has an array pointer passed it to modify stuff in an array:

  • (void) arrayFunction:(Byte[])targetarray { // do stuff to targetarray }

It's an array of type Byte, but I don't think that I've put the right thing in the round brackets. What should it be instead of (Byte[])? There may be several arrays of different sizes passed to this function

Thanks in advance!

2 Answers 2

5

if it's a plain-old array, I would just do this:

(void)arrayFunction:(Byte*)targetarray

Or, to be more "OO-ish", use NSData instead of a byte array:

(void)arrayFunction:(NSData*)targetarray
Sign up to request clarification or add additional context in comments.

2 Comments

Don't forget to pass the array size as a parameter to avoid buffer overflows!
Passing an NSData instance would be overkill in most cases, unless you intend to do something else with it afterwards which would benefit from the NSData wrapper (i.e. dropping it into a dictionary, NSUserDefaults, NSArchiver, etc). Otherwise, just pass the C-style array. For that purpose, just pass Byte , but as Adam pointed out you should really use - (void)arrayFunction:(Byte)targetArray length:(NSUInteger)length;
1

It looks like you're using the plain C array. Remember that array pointers are simply pointers to the first element in the array. You don't pass the "whole array" as a reference, you'll just pass the pointer at index 0.

If you're passing the array, you should define your parameter as a pointer, Byte*, because that's what it really is when you pass a simple C array.

Comments

Your Answer

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