Pass uint8_t array to method

2019-09-12 07:59发布

问题:

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];