Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7419661
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 29, 20262026-05-29T08:06:33+00:00 2026-05-29T08:06:33+00:00

I’ve defined a few macros that make it simpler to define an array of

  • 0

I’ve defined a few macros that make it simpler to define an array of structures, but I can’t find a way to use them without generating errors. Here are the macros (and a few example structures to demonstrate why the macros might be used (the actual structures I’m populating are a little more complex)):

struct string_holder {
    const char *string;
};
struct string_array_holder {
    struct string_holder *holders;
};
#define DEFINE_STRING_ARRAY_HOLDER(name, values) \
    static struct string_holder name##__array[] = values; \
    static struct string_array_holder name = { name##__array }
#define WRAP_STRING(string) { string }

It works just fine when you use it to declare an array with one item:

DEFINE_STRING_ARRAY_HOLDER(my_string_array_holder, {
    WRAP_STRING("my string")
});

But when I use multiple items:

DEFINE_STRING_ARRAY_HOLDER(my_string_array_holder, {
    WRAP_STRING("hello"),
    WRAP_STRING("world")
});

I get this error:

error: too many arguments provided to function-like macro invocation

So it’s interpreting the comma in the braces as an argument separator. I follow the advice from this question and put parentheses around the problematic argument:

DEFINE_STRING_ARRAY_HOLDER(my_string_array_holder, ({
    WRAP_STRING("hello"),
    WRAP_STRING("world")
}));

Now when I try to compile it, it interprets ({ ... }) as a statement expression and complains:

warning: use of GNU statement expression extension
(a bunch of syntax errors resulting from its interpretation as a statement expression)
error: statement expression not allowed at file scope

How can I either:

  • Use the macro without errors (preferred), or
  • Rewrite the macro[s] to work in these circumstances?
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-29T08:06:34+00:00Added an answer on May 29, 2026 at 8:06 am

    Dmitri is right, variadic macros are the way to go.

    I put some example code I use to test if given key is member of a list of values:

    #define _IN(KEY, ...)                                             \
    ({                                                                \
      typedef __typeof(KEY) _t;                                       \
      const _t _key = (KEY);                                          \
      const _t _values[] = { __VA_ARGS__ };                           \
      _Bool _r = 0;                                                   \
      unsigned int _i;                                                \
      for (_i = 0; _i < sizeof(_values) / sizeof(_values[0]); ++_i) { \
        if (_key == _values[_i]) {                                    \
          _r = 1;                                                     \
          break;                                                      \
        }                                                             \
      }                                                               \
      _r;                                                             \
    })
    

    Mind the usage of __VA_ARGS__.

    Update:
    A crude solution if you don’t like __VA_ARGS__ at arbitrary places would be an “unwrapper” macro:

    #define UNWRAP(...) __VA_ARGS__
    

    You could use it like a prefix-operator. 😉

    #include <stdio.h>
    
    /* "unwrapper": */
    #define UNWRAP(...) __VA_ARGS__
    
    /* your macros: */
    #define WRAP(NAME, ELEMS) static const char *NAME[] = { UNWRAP ELEMS }
    
    int main(void) {
      WRAP(some_test, ("a", "b", "c"));
      printf("The second elem in some_test is: '%s'\n", some_test[1]);
      return 0;
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Seemingly simple, but I cannot find anything relevant on the web. What is the
I have a French site that I want to parse, but am running into
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
link Im having trouble converting the html entites into html characters, (&# 8217;) i
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I want to count how many characters a certain string has in PHP, but
I am trying to understand how to use SyndicationItem to display feed which is
I have a jquery bug and I've been looking for hours now, I can't
I've got a string that has curly quotes in it. I'd like to replace
I want use html5's new tag to play a wav file (currently only supported

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.