Search code examples
objective-ccuint8t

uint8_t array with dynamic variable


I have an uint8_t array:

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

And I need the array that has 5 of those theArrays, but first value must change by one. 0x00, 0x01, 0x02 and so on. How can I build that array without rewriting theArray multiple times?


Solution

  • Simply copy an array and change the first value.

    uint8_t array[5][12] = {
                             { 0x00, 0x01,0x00,0x00,0x00,0x06,0xFE,0x03,0x01,0xC1,0x00,0x01 }
                           } ;
    
    for (int i = 1; i < 5; i++ )
    {
      array[i][0] = i;
      for (int j = 1; j < 12; j++ )
      {
        array[i][j] = array[0][j];
      }
    }
    

    Typed in Safari.