How to define a define that defines some defines ?

Discussion in 'C Programming' started by theotyflos, Feb 19, 2004.

  1. theotyflos

    theotyflos Guest

    Hi all,

    I have the following:

    /*--- SNIP ---*/

    typedef struct Argument_s
    {
    char *address;
    int type;
    int length;
    } ARGUMENT;

    #define Function(F) int F( int ArgCount, ARGUMENT ArgVector[] )

    #define First ArgVector[0]
    #define First_A First.address
    #define First_T First.type
    #define First_L First.length
    #define Second ArgVector[1]
    #define Second_A Second.address
    #define Second_T Second.type
    #define Second_L Second.length
    #define Third ArgVector[2]
    #define Third_A Third.address
    #define Third_T Third.type
    #define Third_L Third.length

    Function(One)
    {
    /* do something with First_A */
    /* do something with First_T */
    /* do something with First_L */
    /* do something with Second_A */
    /* do something with Second_T */
    /* do something with Second_L */
    /* do something with Third_A */
    /* do something with Third_T */
    /* do something with Third_L */
    /* etc etc */
    }

    #define Apple ArgVector[0]
    #define Apple_A Apple.address
    #define Apple_T Apple.type
    #define Apple_L Apple.length
    #define Orange ArgVector[1]
    #define Orange_A Orange.address
    #define Orange_T Orange.type
    #define Orange_L Orange.length

    Function(Two)
    {
    /* do something with Apple_A */
    /* do something with Apple_T */
    /* do something with Apple_L */
    /* do something with Orange_A */
    /* do something with Orange_T */
    /* do something with Orange_L */
    /* etc etc */
    }

    /*--- SNIP ---*/

    The question is: Is there a way instead of writing all those #defines
    for the arguments (First, Second, etc), to write something like:

    #define Arg(n) #define Arg ArgVector[n] \
    #define Arg##_A Arg.address \
    #define Arg##_T Arg.type \
    #define Arg##_L Arg.length

    and define the arguments like this:

    Arg First(0)
    Arg Second(1)
    Arg Third(2)
    Function(One)
    .....
    Arg Apple(0)
    Arg Orange(1)
    Function(Two)
    .....


    Thanks in advance everyone.
     
    theotyflos, Feb 19, 2004
    #1
    1. Advertising

  2. theotyflos <> scribbled the following:
    > typedef struct Argument_s
    > {
    > char *address;
    > int type;
    > int length;
    > } ARGUMENT;


    > #define Function(F) int F( int ArgCount, ARGUMENT ArgVector[] )


    > #define First ArgVector[0]
    > #define First_A First.address
    > #define First_T First.type
    > #define First_L First.length
    > #define Second ArgVector[1]
    > #define Second_A Second.address
    > #define Second_T Second.type
    > #define Second_L Second.length
    > #define Third ArgVector[2]
    > #define Third_A Third.address
    > #define Third_T Third.type
    > #define Third_L Third.length


    > #define Apple ArgVector[0]
    > #define Apple_A Apple.address
    > #define Apple_T Apple.type
    > #define Apple_L Apple.length
    > #define Orange ArgVector[1]
    > #define Orange_A Orange.address
    > #define Orange_T Orange.type
    > #define Orange_L Orange.length


    > The question is: Is there a way instead of writing all those #defines
    > for the arguments (First, Second, etc), to write something like:


    > #define Arg(n) #define Arg ArgVector[n] \
    > #define Arg##_A Arg.address \
    > #define Arg##_T Arg.type \
    > #define Arg##_L Arg.length


    > and define the arguments like this:


    > Arg First(0)
    > Arg Second(1)
    > Arg Third(2)
    > Function(One)
    > ....
    > Arg Apple(0)
    > Arg Orange(1)
    > Function(Two)
    > ....


    No. The preprocessor is a one-pass utility. It is impossible for it to
    #define its own directives.

    --
    /-- Joona Palaste () ------------- Finland --------\
    \-- http://www.helsinki.fi/~palaste --------------------- rules! --------/
    "War! Huh! Good God, y'all! What is it good for? We asked Mayor Quimby."
    - Kent Brockman
     
    Joona I Palaste, Feb 19, 2004
    #2
    1. Advertising

  3. theotyflos

    Dan Pop Guest

    In <c12d21$1g6$> Joona I Palaste <> writes:

    >No. The preprocessor is a one-pass utility. It is impossible for it to
    >#define its own directives.


    But it (usually) can be invoked twice, the second invocation processing
    the output of the first one. However, I have yet to find a *good*
    reason for this approach...

    Dan
    --
    Dan Pop
    DESY Zeuthen, RZ group
    Email:
     
    Dan Pop, Feb 19, 2004
    #3
  4. theotyflos wrote:

    > Hi all,
    >
    > I have the following:
    >
    > /*--- SNIP ---*/
    >
    > typedef struct Argument_s
    > {
    > char *address;
    > int type;
    > int length;
    > } ARGUMENT;
    >
    > #define Function(F) int F( int ArgCount, ARGUMENT ArgVector[] )
    >
    > #define First ArgVector[0]
    > #define First_A First.address
    > #define First_T First.type
    > #define First_L First.length
    > #define Second ArgVector[1]
    > #define Second_A Second.address
    > #define Second_T Second.type
    > #define Second_L Second.length
    > #define Third ArgVector[2]
    > #define Third_A Third.address
    > #define Third_T Third.type
    > #define Third_L Third.length
    >
    > Function(One)
    > {
    > /* do something with First_A */
    > /* do something with First_T */
    > /* do something with First_L */
    > /* do something with Second_A */
    > /* do something with Second_T */
    > /* do something with Second_L */
    > /* do something with Third_A */
    > /* do something with Third_T */
    > /* do something with Third_L */
    > /* etc etc */
    > }
    >
    > #define Apple ArgVector[0]
    > #define Apple_A Apple.address
    > #define Apple_T Apple.type
    > #define Apple_L Apple.length
    > #define Orange ArgVector[1]
    > #define Orange_A Orange.address
    > #define Orange_T Orange.type
    > #define Orange_L Orange.length
    >
    > Function(Two)
    > {
    > /* do something with Apple_A */
    > /* do something with Apple_T */
    > /* do something with Apple_L */
    > /* do something with Orange_A */
    > /* do something with Orange_T */
    > /* do something with Orange_L */
    > /* etc etc */
    > }
    >
    > /*--- SNIP ---*/
    >
    > The question is: Is there a way instead of writing all those #defines
    > for the arguments (First, Second, etc), to write something like:
    >
    > #define Arg(n) #define Arg ArgVector[n] \
    > #define Arg##_A Arg.address \
    > #define Arg##_T Arg.type \
    > #define Arg##_L Arg.length
    >
    > and define the arguments like this:
    >
    > Arg First(0)
    > Arg Second(1)
    > Arg Third(2)
    > Function(One)
    > ....
    > Arg Apple(0)
    > Arg Orange(1)
    > Function(Two)
    > ....
    >
    >
    > Thanks in advance everyone.


    Looks like a better design is to have a function that
    accesses the structures:
    void Set_Arguments(struct Argument_s * arg,
    char * address,
    int type,
    int length)
    {
    arg->address = address;
    arg->type = type;
    arg->length = length;
    return;
    }

    One could "Take it up a notch" by using constant identifiers:
    #if NO_ENUMERATIONS
    #define APPLE 0
    #define ORANGE 1
    #define WATERMELON 2
    #else
    enum {APPLE, ORANGE, WATERMELON};
    #endif

    struct Argument_s fruits[5];

    /* ... */
    Set_Arguments(&fruit[APPLE], "hello", 1, 25);


    --
    Thomas Matthews

    C++ newsgroup welcome message:
    http://www.slack.net/~shiva/welcome.txt
    C++ Faq: http://www.parashift.com/c -faq-lite
    C Faq: http://www.eskimo.com/~scs/c-faq/top.html
    alt.comp.lang.learn.c-c++ faq:
    http://www.raos.demon.uk/acllc-c /faq.html
    Other sites:
    http://www.josuttis.com -- C++ STL Library book
     
    Thomas Matthews, Feb 19, 2004
    #4
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Replies:
    5
    Views:
    3,459
    Brian Drummond
    Feb 24, 2006
  2. Rob R. Ainscough
    Replies:
    1
    Views:
    416
    =?Utf-8?B?RGVhc3Vu?=
    Jun 27, 2005
  3. John Leonard
    Replies:
    2
    Views:
    386
    Rick Genter
    Sep 28, 2003
  4. exquisitus
    Replies:
    12
    Views:
    22,721
    Tilman Bohn
    Feb 19, 2005
  5. badc0de

    how do I let #define defines a macro

    badc0de, Oct 1, 2007, in forum: C Programming
    Replies:
    2
    Views:
    306
    badc0de
    Oct 1, 2007
Loading...

Share This Page