I found this way of resolving array indexes in a code base. Does anyone know how this is able to resolve this define to the appropriate array index?
Code:
#include <stdio.h>
#include <stdlib.h>
#define PARAMS1 1,1,0
#define PARAMS2 1,1,2
int main()
{
u_int16_t temp[4] = {0};
temp[1,1,0 / 2] = 5;
temp[1,1,2 / 2] = 6;
printf("temp1 index %d\n", (1,1,0)/2); // resolves to index 0
printf("temp2 index %d\n", (1,1,2)/2); // resolves to index 1
for(int i = 0; i < sizeof(temp)/(sizeof(u_int16_t)); i++)
{
printf("temp[%d] = %d\n", i, temp[i]);
}
return 0;
}
Output:
temp1 index 0
temp2 index 1
temp[0] = 5
temp[1] = 6
temp[2] = 0
temp[3] = 0
Edit:
It looks like this syntax returns the last value in the list. If anyone can add additional background on what the compiler does with this syntax it would be much appreciated!