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

  • SEARCH
  • Home
  • 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 483243
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T01:10:39+00:00 2026-05-13T01:10:39+00:00

I was wondering if it is possible to iterate over arguments passed to a

  • 0

I was wondering if it is possible to iterate over arguments passed to a variadic macro in C99 or using any GCC extensions ?

For e.g. is it possible to write a generic macro that takes a structure and its fields passed as arguments and prints offset of each field within the structure ?

Something like this:

struct a {
    int a;
    int b;
    int c;
};

/* PRN_STRUCT_OFFSETS will print offset of each of the fields 
   within structure passed as the first argument.
*/

int main(int argc, char *argv[])
{
    PRN_STRUCT_OFFSETS(struct a, a, b, c);

    return 0;
}
  • 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-13T01:10:39+00:00Added an answer on May 13, 2026 at 1:10 am

    Here is my homework of the day, it’s based on macro tricks and today I particularly learnt about __VA_NARG__ invented by Laurent Deniau. Anyway, the following sample code works up to 8 fields for the sake of clarity. Just extend the code by duplicating if you need more (this is because the preprocessor doesn’t feature recursion, as it reads the file only once).

    #include <stdio.h>
    #include <stddef.h>
    
    struct a
    {
      int a;
      int b;
      int c;
    };
    
    struct b
    {
      int a;
      int b;
      int c;
      int d;
    };
    
    #define STRINGIZE(arg)  STRINGIZE1(arg)
    #define STRINGIZE1(arg) STRINGIZE2(arg)
    #define STRINGIZE2(arg) #arg
    
    #define CONCATENATE(arg1, arg2)   CONCATENATE1(arg1, arg2)
    #define CONCATENATE1(arg1, arg2)  CONCATENATE2(arg1, arg2)
    #define CONCATENATE2(arg1, arg2)  arg1##arg2
    
    /* PRN_STRUCT_OFFSETS will print offset of each of the fields 
     within structure passed as the first argument.
     */
    #define PRN_STRUCT_OFFSETS_1(structure, field, ...) printf(STRINGIZE(structure)":"STRINGIZE(field)"-%d\n", offsetof(structure, field));
    #define PRN_STRUCT_OFFSETS_2(structure, field, ...)\
      printf(STRINGIZE(structure)":"STRINGIZE(field)"-%d\n", offsetof(structure, field));\
      PRN_STRUCT_OFFSETS_1(structure, __VA_ARGS__)
    #define PRN_STRUCT_OFFSETS_3(structure, field, ...)\
      printf(STRINGIZE(structure)":"STRINGIZE(field)"-%d\n", offsetof(structure, field));\
      PRN_STRUCT_OFFSETS_2(structure, __VA_ARGS__)
    #define PRN_STRUCT_OFFSETS_4(structure, field, ...)\
      printf(STRINGIZE(structure)":"STRINGIZE(field)"-%d\n", offsetof(structure, field));\
      PRN_STRUCT_OFFSETS_3(structure, __VA_ARGS__)
    #define PRN_STRUCT_OFFSETS_5(structure, field, ...)\
      printf(STRINGIZE(structure)":"STRINGIZE(field)"-%d\n", offsetof(structure, field));\
     PRN_STRUCT_OFFSETS_4(structure, __VA_ARGS__)
    #define PRN_STRUCT_OFFSETS_6(structure, field, ...)\
      printf(STRINGIZE(structure)":"STRINGIZE(field)"-%d\n", offsetof(structure, field));\
      PRN_STRUCT_OFFSETS_5(structure, __VA_ARGS__)
    #define PRN_STRUCT_OFFSETS_7(structure, field, ...)\
      printf(STRINGIZE(structure)":"STRINGIZE(field)"-%d\n", offsetof(structure, field));\
      PRN_STRUCT_OFFSETS_6(structure, __VA_ARGS__)
    #define PRN_STRUCT_OFFSETS_8(structure, field, ...)\
      printf(STRINGIZE(structure)":"STRINGIZE(field)"-%d\n", offsetof(structure, field));\
      PRN_STRUCT_OFFSETS_7(structure, __VA_ARGS__)
    
    #define PRN_STRUCT_OFFSETS_NARG(...) PRN_STRUCT_OFFSETS_NARG_(__VA_ARGS__, PRN_STRUCT_OFFSETS_RSEQ_N())
    #define PRN_STRUCT_OFFSETS_NARG_(...) PRN_STRUCT_OFFSETS_ARG_N(__VA_ARGS__) 
    #define PRN_STRUCT_OFFSETS_ARG_N(_1, _2, _3, _4, _5, _6, _7, _8, N, ...) N 
    #define PRN_STRUCT_OFFSETS_RSEQ_N() 8, 7, 6, 5, 4, 3, 2, 1, 0
    
    #define PRN_STRUCT_OFFSETS_(N, structure, field, ...) CONCATENATE(PRN_STRUCT_OFFSETS_, N)(structure, field, __VA_ARGS__)
    
    #define PRN_STRUCT_OFFSETS(structure, field, ...) PRN_STRUCT_OFFSETS_(PRN_STRUCT_OFFSETS_NARG(field, __VA_ARGS__), structure, field, __VA_ARGS__)
    
    int main(int argc, char *argv[])
    {
      PRN_STRUCT_OFFSETS(struct a, a, b, c);
      printf("\n");
      PRN_STRUCT_OFFSETS(struct b, a, b, c, d);
    
      return 0;
    }
    

    which prints out:

    struct a:a-0
    struct a:b-4
    struct a:c-8
    
    struct b:a-0
    struct b:b-4
    struct b:c-8
    struct b:d-12
    

    EDIT: Here is a slightly different version that tries to be more generic. The FOR_EACH(what, ...) macro applies what to every other argument in the variable argument list.

    So, you just have to define a macro that takes a single argument like this:

    #define DO_STUFF(x) foo(x)
    

    which is going to be applied to every argument in the list.
    So, for your typical example you need to hack a bit but it still remains concise:

    #define PRN_STRUCT_OFFSETS_(structure, field) printf(STRINGIZE(structure)":"STRINGIZE(field)" - offset = %d\n", offsetof(structure, field));
    #define PRN_STRUCT_OFFSETS(field) PRN_STRUCT_OFFSETS_(struct a, field)
    

    And you apply it like this:

    FOR_EACH(PRN_STRUCT_OFFSETS, a, b, c);
    

    Finally, a complete sample program:

    #include <stdio.h>
    #include <stddef.h>
    
    struct a
    {
      int a;
      int b;
      int c;
    };
    
    #define STRINGIZE(arg)  STRINGIZE1(arg)
    #define STRINGIZE1(arg) STRINGIZE2(arg)
    #define STRINGIZE2(arg) #arg
    
    #define CONCATENATE(arg1, arg2)   CONCATENATE1(arg1, arg2)
    #define CONCATENATE1(arg1, arg2)  CONCATENATE2(arg1, arg2)
    #define CONCATENATE2(arg1, arg2)  arg1##arg2
    
    #define FOR_EACH_1(what, x, ...) what(x)
    #define FOR_EACH_2(what, x, ...)\
      what(x);\
      FOR_EACH_1(what,  __VA_ARGS__);
    #define FOR_EACH_3(what, x, ...)\
      what(x);\
      FOR_EACH_2(what, __VA_ARGS__);
    #define FOR_EACH_4(what, x, ...)\
      what(x);\
      FOR_EACH_3(what,  __VA_ARGS__);
    #define FOR_EACH_5(what, x, ...)\
      what(x);\
     FOR_EACH_4(what,  __VA_ARGS__);
    #define FOR_EACH_6(what, x, ...)\
      what(x);\
      FOR_EACH_5(what,  __VA_ARGS__);
    #define FOR_EACH_7(what, x, ...)\
      what(x);\
      FOR_EACH_6(what,  __VA_ARGS__);
    #define FOR_EACH_8(what, x, ...)\
      what(x);\
      FOR_EACH_7(what,  __VA_ARGS__);
    
    #define FOR_EACH_NARG(...) FOR_EACH_NARG_(__VA_ARGS__, FOR_EACH_RSEQ_N())
    #define FOR_EACH_NARG_(...) FOR_EACH_ARG_N(__VA_ARGS__) 
    #define FOR_EACH_ARG_N(_1, _2, _3, _4, _5, _6, _7, _8, N, ...) N 
    #define FOR_EACH_RSEQ_N() 8, 7, 6, 5, 4, 3, 2, 1, 0
    
    #define FOR_EACH_(N, what, x, ...) CONCATENATE(FOR_EACH_, N)(what, x, __VA_ARGS__)
    #define FOR_EACH(what, x, ...) FOR_EACH_(FOR_EACH_NARG(x, __VA_ARGS__), what, x, __VA_ARGS__)
    
    #define PRN_STRUCT_OFFSETS_(structure, field) printf(STRINGIZE(structure)":"STRINGIZE(field)" - offset = %d\n", offsetof(structure, field));
    #define PRN_STRUCT_OFFSETS(field) PRN_STRUCT_OFFSETS_(struct a, field)
    
    int main(int argc, char *argv[])
    {
      FOR_EACH(PRN_STRUCT_OFFSETS, a, b, c);
      printf("\n");
    
      return 0;
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm wondering how to iterate over a List with mixed contents using foreach. See
I was wondering if is it possible to iterate trough all arrays elements starting
I was wondering if it possible to add constants to php before any scripts
I was wondering ( if possible ) if there was a program/tool/utility that when
Just toying with the SDK and I was wondering if possible a UITouch event
I was wondering if its possible to use the Google maps IFRAME code to
I was wondering - is it possible to change files that are stored on
I was wondering is it possible to register a broadcast receiver to receive two
I am wondering if its possible to to open a port listen for a
I was wondering if its possible to add custom header with classic asp. In

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.