Search code examples
objective-ccuint8t

Array of uint8_t arrays


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

and I want them to add to another array:

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

but the values of arrays change when I add them to theArray.

Why? How could I add them to array properly?


Solution

  • The toplevel array should just be an array of pointers:

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

    You will lose information about the lengths of each "row", but that's fine.

    I don't think you can reference an array in the initializer like in your example and have the elements copied into a larger array automatically.