简体   繁体   English

如何通过引用函数传递数组?

[英]How to pass an array in by reference to a function?

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 } (void)arrayFunction :( Byte [])targetarray {//做东西到targetarray}

It's an array of type Byte, but I don't think that I've put the right thing in the round brackets. 它是Byte类型的数组,但我认为我没有把正确的东西放在圆括号中。 What should it be instead of (Byte[])? 应该是什么而不是(Byte [])? There may be several arrays of different sizes passed to this function 可能有几个不同大小的数组传递给此函数

Thanks in advance! 提前致谢!

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: 或者,为了更“OO-ish”,使用NSData而不是字节数组:

(void)arrayFunction:(NSData*)targetarray

It looks like you're using the plain C array. 看起来你正在使用普通的C数组。 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. 您没有将“整个数组”作为引用传递,您只需将指针传递给索引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. 如果要传递数组,则应将参数定义为指针Byte* ,因为这是传递简单C数组时的实际情况。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM