Probably a naïve question - I used to program 20 years ago and haven't coded much since. My memory of how the C preprocessor
works has atrophied significantly since then...
I am writing a very simple C
program and I am trying to declare a few static global arrays, but the size of the arrays
would be dependent (on a non-trivial way) on a MODE
variable. Something like the simplified example below.
Two quick points: I know I could just size the arrays
according to the largest size needed by any MODE
, but I don't want to that because (unlike in the simplified example below) sometimes a handful of these dimensions are going to be extremely large while others are tiny.
Also, I want to use statically defined global arrays - rather than dynamically allocate them at runtime. I want the compiler to have the sizes at compile time.
//** Simplified example of what I'd like to do **//
#define SIZE_LIST_1[5] = {2, 7, 23, 33, 12, 76} // I don't think this is valid syntax
#define SIZE_LIST_2[5] = {11, 65, 222, 112, 444}
#define MODE 4
#define S1 SIZE_LIST_1[MODE]
#define S2 SIZE_LIST_2[MODE]
int a[S1], b[S2];
An array is a collection of elements of the same type placed in contiguous memory locations that can be individually referenced by using an index to a unique identifier. Five values of type int can be declared as an array without having to declare five different variables (each with its own identifier).
For example, they can picture a marching band arranged in equal rows or chairs set up evenly in an auditorium. In both cases, they are visualizing rows and columns. An arrangement of objects, pictures, or numbers in rows and columns is called an array.
There are three different kinds of arrays: indexed arrays, multidimensional arrays, and associative arrays.
An array type is a user-defined data type consisting of an ordered set of elements of a single data type. An ordinary array type has a defined upper bound on the number of elements and uses the ordinal position as the array index.
You need to define a bunch of helper macros first before you can do this in a simple way:
#define CONCAT(A,B) A ## B
#define EXPAND_CONCAT(A,B) CONCAT(A, B)
#define ARGN(N, LIST) EXPAND_CONCAT(ARG_, N) LIST
#define ARG_0(A0, ...) A0
#define ARG_1(A0, A1, ...) A1
#define ARG_2(A0, A1, A2, ...) A2
#define ARG_3(A0, A1, A2, A3, ...) A3
#define ARG_4(A0, A1, A2, A3, A4, ...) A4
#define ARG_5(A0, A1, A2, A3, A4, A5, ...) A5
#define ARG_6(A0, A1, A2, A3, A4, A5, A6, ...) A6
#define ARG_7(A0, A1, A2, A3, A4, A5, A6, A7, ...) A7
#define ARG_8(A0, A1, A2, A3, A4, A5, A6, A7, A8, ...) A8
#define ARG_9(A0, A1, A2, A3, A4, A5, A6, A7, A8, A9, ...) A9
#define ARG_10(A0, A1, A2, A3, A4, A5, A6, A7, A8, A9, A10, ...) A10
/* above should be in a pp_helper.h header file or some such */
#define SIZE_LIST_1 ( 2, 7, 23, 33, 12, 76)
#define SIZE_LIST_2 (11, 65, 222, 112, 444, 1000)
#define S1 ARGN(MODE, SIZE_LIST_1)
#define S2 ARGN(MODE, SIZE_LIST_2)
#define MODE 4
int a[S1], b[S2];
There are a bunch of preprocessor 'libraries' you can get with the boilerplate code (boost PP, P99), or you can just roll your own. The main problem being that you need to define ARG macros based on the largest number of arguments you'll ever want to handle.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With