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

时间:2021-01-31 22:55:03

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. What should it be instead of (Byte[])? There may be several arrays of different sizes passed to this function

它是Byte类型的数组,但我认为我没有把正确的东西放在圆括号中。应该是什么而不是(Byte [])?可能有几个不同大小的数组传递给此函数

Thanks in advance!

提前致谢!

2 个解决方案

#1


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

#2


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.

看起来你正在使用普通的C数组。请记住,数组指针只是指向数组中第一个元素的指针。你没有传递“整个数组”作为参考,你只需将指针传递给索引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数组时的实际情况。

#1


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

#2


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.

看起来你正在使用普通的C数组。请记住,数组指针只是指向数组中第一个元素的指针。你没有传递“整个数组”作为参考,你只需将指针传递给索引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数组时的实际情况。