D
dan
I would like to have the preprocessor automatically generate the
number of array elements requested. Each element is zero. The elements
get pasted into a larger array. The other elements may be non-zero.
***** Here is an example of what I need to do:
#define YEAR_1 2005
#define YEAR_2 2007
#define YEARS (YEAR_2 - YEAR_1 + 1)
// Start missing steps (using ## operator in some way?)
// This is the part I can't figure out.
// I think the following definitions are probably needed in the
missing steps
// I'd like ZEROES_AND_COMMAS (YEARS) to get translated to one of them
#define ZEROES_AND_COMMAS_1 0
#define ZEROES_AND_COMMAS_2 0, 0
#define ZEROES_AND_COMMAS_3 0, 0, 0
// End missing steps
#define ARRAY_ELEMS_AGE 0, 1, 5, 10, 15, 20, 40, 60
#define ARRAY_ELEMS_YRS ZEROES_AND_COMMAS (YEARS)
#define ARRAY_ELEMS_EDU 0, 6, 8, 12, 16
int intArray [] = { ARRAY_ELEMS_AGE , ARRAY_ELEMS_YRS ,
ARRAY_ELEMS_EDU , };
// intArray ends up with { 0, 1, 5, 10, 15, 20, 40, 60, 0, 0, 0,
0, 6, 8, 12, 16, }
****** Here's one failed test program (does not compile)
#include <stdio.h>
#include <stdlib.h>
#define YEAR_1 2005
#define YEAR_2 2007
#define YEARS (YEAR_2 - YEAR_1 + 1)
#define CAT(x,y) x ## y
#define XCAT(x,y) CAT(x,y)
#define ZEROES_AND_COMMAS_1 0
#define ZEROES_AND_COMMAS_2 0, 0
#define ZEROES_AND_COMMAS_3 0, 0, 0
#define ZEROES_AND_COMMAS(n) XCAT(ZEROES_AND_COMMAS_, n)
main () {
int intArray [] = { ZEROES_AND_COMMAS (YEARS) };
printf ("Number of elements = %d\n", sizeof (intArray) / sizeof
(int));
}
This would actually be useful. I'm very familiar with the C
preprocessor. I've experimented with the ## operator a lot, and can't
make it do the above task, including experiments related to K+R page
231 about using a second level of macro definition. Can anyone explain
how to solve this problem? Or get the test program to compile and
produce correct array length output?
Thanks,
Daniel Goldman
number of array elements requested. Each element is zero. The elements
get pasted into a larger array. The other elements may be non-zero.
***** Here is an example of what I need to do:
#define YEAR_1 2005
#define YEAR_2 2007
#define YEARS (YEAR_2 - YEAR_1 + 1)
// Start missing steps (using ## operator in some way?)
// This is the part I can't figure out.
// I think the following definitions are probably needed in the
missing steps
// I'd like ZEROES_AND_COMMAS (YEARS) to get translated to one of them
#define ZEROES_AND_COMMAS_1 0
#define ZEROES_AND_COMMAS_2 0, 0
#define ZEROES_AND_COMMAS_3 0, 0, 0
// End missing steps
#define ARRAY_ELEMS_AGE 0, 1, 5, 10, 15, 20, 40, 60
#define ARRAY_ELEMS_YRS ZEROES_AND_COMMAS (YEARS)
#define ARRAY_ELEMS_EDU 0, 6, 8, 12, 16
int intArray [] = { ARRAY_ELEMS_AGE , ARRAY_ELEMS_YRS ,
ARRAY_ELEMS_EDU , };
// intArray ends up with { 0, 1, 5, 10, 15, 20, 40, 60, 0, 0, 0,
0, 6, 8, 12, 16, }
****** Here's one failed test program (does not compile)
#include <stdio.h>
#include <stdlib.h>
#define YEAR_1 2005
#define YEAR_2 2007
#define YEARS (YEAR_2 - YEAR_1 + 1)
#define CAT(x,y) x ## y
#define XCAT(x,y) CAT(x,y)
#define ZEROES_AND_COMMAS_1 0
#define ZEROES_AND_COMMAS_2 0, 0
#define ZEROES_AND_COMMAS_3 0, 0, 0
#define ZEROES_AND_COMMAS(n) XCAT(ZEROES_AND_COMMAS_, n)
main () {
int intArray [] = { ZEROES_AND_COMMAS (YEARS) };
printf ("Number of elements = %d\n", sizeof (intArray) / sizeof
(int));
}
This would actually be useful. I'm very familiar with the C
preprocessor. I've experimented with the ## operator a lot, and can't
make it do the above task, including experiments related to K+R page
231 about using a second level of macro definition. Can anyone explain
how to solve this problem? Or get the test program to compile and
produce correct array length output?
Thanks,
Daniel Goldman