Pass uint8_t array to method

六月ゝ 毕业季﹏ 提交于 2019-12-13 20:15:51

问题


I have four uint8_t arrays:

uint8_t arrayOne[12]   = { 0x00,0x01,0x00,0x00,0x00,0x06,0xFE,0x03,0x01,0xC1,0x00,0x01 };

uint8_t arrayTwo[12]   = { 0x00,0x01,0x00,0x00,0x00,0x06,0xFE,0x03,0x4E,0x2D,0x00,0x0C };

uint8_t arrayThree[12] = { 0x00,0x01,0x00,0x00,0x00,0x06,0xFE,0x03,0x01,0xF3,0x00,0x01 };

uint8_t arrayFour[12]  = { 0x00,0x01,0x00,0x00,0x00,0x06,0xFE,0x03,0x20,0x04,0x00,0x01 };

I have added them to array:

uint8_t *theArray[] = { arrayOne,arrayTwo,arrayThree,arrayFour };

now I want to pass this array to a method, for example:

[self theMethod:theArray];

to:

-(void)theMethod:(uint8_t *)pointersArray[]{
...
...
}

What is the proper way to point that array to a method in -(void)theMethod... ?


回答1:


This line:

uint8_t *theArray = { arrayOne,arrayTwo,arrayThree,arrayFour };

is actually creating an array filled in with the pointers to your arrays converted to uint8_t values. I don't think this is what you want.

So first of all (notice the double pointer):

uint8_t *theArray[] = { arrayOne,arrayTwo,arrayThree,arrayFour };

Then your ObjC method becomes:

-(void)theMethod:(uint8_t **)pointersArray {

}



回答2:


Declare your method to take in a pointer to array pointers

-(void)theMethod:(uint8_t **)pointersArray{
...
...
}

Invoke the method by passing it like this like

[self theMethod:&theArray];


来源:https://stackoverflow.com/questions/30618860/pass-uint8-t-array-to-method

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!