Is there a way to determine a position within a const array at compile-time in c? Here's an example of what I'm trying to do:
const unsigned char DATA[] = {
// Part 1
// hundreds or thousands of values
// Part 2
// compiler records this position in the array in PART2
// hundreds or thousands of values
// Part 3
// compiler records this position in the array in PART3
// hundreds or thousands of values
// ...
};
const unsigned int PART_IDX [] = {
// index to Part 1
0,
// index to Part 2
PART2,
// index to Part 3
PART3,
// ...
};
I could calculate the indexes at run-time, but would rather have them already done, since the array is constant. I could make a program that analyzes the source code, counts the number of elements in each part, and inserts the data in PART_IDX, but I would really like to have the compiler do this at compile-time. That way if data gets inserted or deleted, or parts get added or removed, the compiler still generates correct code. Does someone know how I could do this? Thanks!
Edit: To clarify, using a example with actual data:
const unsigned char DATA[] = {
// Part 1
0, 1, 2, 3, 4,
// Part 2
// compiler records this position in the array in PART2 (should be 5)
10, 11, 12, 13, 14, 15, 16,
// Part 3
// compiler records this position in the array in PART3 (should be 12)
20, 21, 22
};
const unsigned int PART_IDX [] = {
// index to Part 1
0,
// index to Part 2
PART2, // should be 5, points to 10 in the array
// index to Part 3
PART3, // should be 12, points to 20 in the array
};
Question is, what can I put in place of the lines that start with // compiler records this position ...
to get the compiler to record the appropriate values in PART2
and PART3
?