I created a function which prints the elements of a table. The table has always 256 elements and the type of element might be: uint8_t / uint16_t or uint32_t. I do an experiment with void argument in function.
void printf_crc_table(void *pt, uint8_t crc_length) {
for (uint8_t i = 0; i < 16; i++) {
for (uint8_t k = 0; k < 16; k++) {
switch (crc_length) {
case 16:
printf("0x%04X, ", *(uint16_t*)pt);
break;
case 32:
printf("0x%08X, ", *(uint32_t*)pt);
break;
default:
printf("0x%02X, ", *(uint8_t*)pt);
break;
}
pt++; //works only for uint8_t. For uint16_t it has to be pt+=2;
}
printf("\n");
}
}
The problem I have is that increasing pointer address+1 works correctly only for uint8_t1 table.
If I call the function with uint16t_t element type, every second value is incorrect. This can be repaired by increasing the pointer's address by 2. But why? And how should I call/re-write the function that it always print correct values?
In main I call the function in a that way:
printf_crc_table(crc8_0x07_t, 8); // works fine
printf_crc_table(crc16_0x1021_t, 16); // every second value is printed incorrectly.
I could use always uint32_t and cast simple uint8_t to uint32_t but I would like to use minimal resources of the micro-controller. Would it be better to use uint32_t instead of void?