You have a very special case there. I believe that EEPROM_A##
be a family of macros. Macros work at the pre-processing level of the source file, a purely textual processing that takes place before calling the C compiler itself. I go into more detail about the preprocessor in this other answer.
In this case, there is not much to be done. At some point you need to access each of these macros... What can be done is to access these macros at an earlier time, populate a vector with these values.
If they are not macros, if they are variables, the language also does not provide any facility in relation to this. C is a compiled language, and the name of the variables serves only as a mnemonic so that the programmer (and the compiler as well) can call a memory region. After the compilation, the name ceases to exist and there is only the memory region, so there is not much to do about it.
In the case of variables, if they were compiled aligned as if they were an array, it is possible to use a few pointers to perform this operation on a loop. Recap: if the variables were compiled aligned as if they were a vector, something that depends on compiling the library from where you get these values. If you are compiling the library or if this is not explicitly written in its documentation, then we can assume that the following method is unreliable.
int i;
int *eeprom_a0_pt;
eeprom_a0_pt = &EEPROM_A0;
for (i = 15; i >= 0; i--) {
H8_3687_pulse(EEPROM_MASK(buf, *(eeprom_a0_pt + i) ));
}
But you can only do this if and only if it is guaranteed that the variables were compiled aligned as if they were an array.
I looked for some alternative using the GCC extension of variadic macros, but I couldn’t find anything that could do this processing.
the section below was written before I became aware of tcc, serving then for traditional C with no external libraries; for more details, see Wtrmute
But why all this? Simple, by the design of the C language.
As I explained above, the name of the variables is lost in the compilation. If it is macro, the text substitution occurs before the compilation.
To do this take the exit from the printf
and execute it correctly, you would need to evaluate (evaluate in English) the expression. Doing this evaluation is the equivalent of calling a compiler for that chunk of code, also known as calling the function eval
available in some programming languages.
The practice of dynamic code generation to be evaluated in run time is typical of Lisp and Bash, not very common in C.
Lacobus, no point! I will choose your answer! What is this solution called with array? Library, List?
– silvio pontes