mercredi 1 juillet 2015

Split array into fixed size

I would like to know how can I split my array into a fixed size ( a block) and each block contains an ID

Example: I have an array of data 1, 2, 3, 4, 5, 6, 7, 8

I would like to output it as

  • Counter 0 = 1, 2, 3
  • Counter 1 = 4, 5, 6
  • Counter 2 = 7, 8

Each block of has an incremented counter

const uint8_t MAX_SIZE = 3;

uint8_t data[] = { 1, 2, 3, 4, 5, 6, 7, 8};
uint8_t counter = 0;

int data_length = sizeof(data);
int current_length = data_length;
int index = 0;

while (current_length > MAX_SIZE) {
    printf("Counter: %d = ", counter++);
    for (int i = 0; i < MAX_SIZE; i++) {
        printf("%d ", data[index++]);
    }

    printf("\n");

    current_length -= MAX_SIZE;
}

printf("Counter: %d = ", counter++);
for (int i = 0; i < current_length; i++) {
    printf("%d ", data[index++]);
}

printf("\n");

Aucun commentaire:

Enregistrer un commentaire