views:

852

answers:

2

Greetings everyone!

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!

+4  A: 

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
Eric Petroelje
Don't forget to pass the array size as a parameter to avoid buffer overflows!
Adam Rosenfield
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;
Jim Dovey
+1  A: 

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.

Kekoa