Preprocessor automation to define/declare array of strings
- From: pozzugno@xxxxxxxxx (pozz)
- Date: Mon, 06 Aug 2012 16:23:53 +0200
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
.
- Follow-Ups:
- Re: Preprocessor automation to define/declare array of strings
- From: Jorgen Grahn
- Re: Preprocessor automation to define/declare array of strings
- From: Ike Naar
- Re: Preprocessor automation to define/declare array of strings
- From: Eric Sosman
- Re: Preprocessor automation to define/declare array of strings
- Prev by Date: Re: Wrapping existing UNIX commands in C
- Next by Date: Re: Wrapping existing UNIX commands in C
- Previous by thread: Re: casting unsigned int to void*
- Next by thread: Re: Preprocessor automation to define/declare array of strings
- Index(es):
Relevant Pages
|