Velocity Reviews

Velocity Reviews (http://www.velocityreviews.com/forums/index.php)
-   C Programming (http://www.velocityreviews.com/forums/f42-c-programming.html)
-   -   Re: Preprocessor automation to define/declare array of strings (http://www.velocityreviews.com/forums/t949154-re-preprocessor-automation-to-define-declare-array-of-strings.html)

Ike Naar 08-06-2012 02:55 PM

Re: Preprocessor automation to define/declare array of strings
 
On 2012-08-06, pozz <pozzugno@gmail.com> wrote:
> 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;


This will not work. The macro takes two parameters,
but it is called with more than two arguments, e.g. in

STRINGLIST(strlist_onoff, { "ON", "OFF" } );

there are three arguments,

strlist_onoff /* first */
{ "ON" /* second */
"OFF" } /* third */

This could be fixed using a variable arguments macro,

#define STRINGLIST(name, ...) const char *name[] = __VA_ARGS__;

Another issue: perhaps it would be better to drop the terminating
semicolon from the macro definition, because if the macro is called
the way you do it, the result will have two terminating semicolons
which may cause syntax errors.


All times are GMT. The time now is 11:19 AM.

Powered by vBulletin®. Copyright ©2000 - 2014, vBulletin Solutions, Inc.
SEO by vBSEO ©2010, Crawlability, Inc.