I have a set of arrays :msg1[]
msg2[]
.... msgn[]
. And I need to use the values in a while
loop. as msgi[]
. When I define it as #define MSG(a) msg##a
and put it in a loop and increment i
, it expands it to msgi
?
相关问题
- Multiple sockets for clients to connect to
- What is the best way to do a search in a large fil
- glDrawElements only draws half a quad
- Index of single bit in long integer (in C) [duplic
- Equivalent of std::pair in C
It can't be done cause macros are replaced at compilation time not runtime, so it will be replaced once...
what you could do is use 2D array if there are in the same size or use array of arrays if there are in different sizes:
No, unfortunately it won't. C does not support runtime name lookups. Instead, you should use a two dimensional array of the form:
This will allow the arrays to be of different sizes and types, although you will have to cast to whatever type the array is.
You can't do it that way. Instead you could create a new array, that contains pointers to the actual arrays:
build your c code with
gcc -E myfile.c
and you will see the reasonthis called preprocessor code. the prprocessor code is the code generated by your compilator before the compilation. in this code the compilator replace the macros in your origin code with the content of the macro.
your origin code:
preprocessr code generated from the origin code (could be seen with
gcc -E
):You can use 2D array instead
No, it will not work that way, because the macro gets expanded before compilation, not after. You'll need a different approach, such as the 2D array suggested by @zakinster.