How to pass an array by reference to a function?

I have a function in which the array pointer passed it to change the material in the array:

  • (void) arrayFunction: (Byte []) targetarray {// Do the material in targetarray}

This is an array of type Byte, but I don’t think I put the right thing in parentheses. What should it be instead of (Byte [])? There may be several arrays of different sizes passed to this function

Thanks in advance!

+4
source share
2 answers

If it is a simple array, I would just do this:

(void)arrayFunction:(Byte*)targetarray 

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

 (void)arrayFunction:(NSData*)targetarray 
+5
source

It looks like you are using a simple array C. Remember that the pointers of the array simply point to the first element in the array. You do not pass the "whole array" as a reference, you just pass the pointer to index 0.

If you are passing an array, you must define your parameter as a pointer, Byte* , because this is what actually happens when you pass a simple array of C.

+1
source

All Articles