Preprocessor automation to define/declare array of strings



I have a lot of array of strings defined in strings.c and declared in
strings.h.

--- strings.c ---
const char *strlist_month[] = { "Jan", "Feb", ... };
const char *strlist_day[] = { "Mon", "Thu", ... };
const char *strlist_onoff[] = { "ON", "OFF" };

--- strings.h ---
extern const char *strlist_month[];
extern const char *strlist_day[];
extern const char *strlist_onoff[];

They are used to write a list of possible values for certain user-defined
parameters:

print_list(const char *strlist[], size_t size) {
size_t i;
for (i = 0; i < size; i++) {
printf("%sn", list[i];
}
}

As you can understand, maintain synchronized strings.c and strings.h can
be tedious: when I add a list in strings.c, I have to remember to add it
in strings.h also and I have to take care to use the same name.

I was thinking to automate the declaration/definition of lists of strings
through C preprocessor. For example:

--- defstrings.def ---
STRINGLIST(strlist_month, { "Jan", "Feb", ... } );
STRINGLIST(strlist_day, { "Mon", "Thu", ... } );
STRINGLIST(strlist_onoff, { "ON", "OFF", ... } );
--- defstrings.c ---
#define STRINGLIST(name, list) const char *name[] = list;
#include "defstrings.def"
--- defstrings.h ---
#define STRINGLIST(name, list) extern const char *name[];

Have you a better suggestion?

--

questo articolo e` stato inviato via web dal servizio gratuito
http://www.newsland.it/news segnala gli abusi ad abuse@xxxxxxxxxxx


.



Relevant Pages

  • Re: RegEx again
    ... Subject: RegEx again ... I got two arrays of strings. ... I am trying to search to see if any of the strings of one array ... Lists to search: ...
    (perl.beginners)
  • RegEx again
    ... I got two arrays of strings. ... I am trying to search to see if any of the strings of one array ... Lists to search: ... qw/brown black blue/ ...
    (perl.beginners)
  • Re: Why these dont work??
    ... everything that works for lists works for strings (the immutable vs ... On the other hand (other than installing NumPy) is there a built-in ... way to do an array full of zeros or one just like the numpy.zeros? ...
    (comp.lang.python)
  • Re: Constant list of strings and numbers
    ... > Is it possible to make a constant list of lists of strings and numbers ... > in Java? ... Your example is not right because all of the values in an array ...
    (comp.lang.java.programmer)
  • Re: RegEx again
    ... I am trying to search to see if any of the strings of one array ... Lists to search: ... foreach my $x { ...
    (perl.beginners)