Preprocessor possibilities

Discussion in 'C Programming' started by Christopher Benson-Manica, Dec 13, 2005.

  1. Is it possible to write a macro (in unextended C89) such that

    TEST( int, (1,2,3) );

    expands to

    int array[]={ 1,2,3 };

    ? I strongly suspect that it is not, but I don't wish to overlook a
    solution if one exists.

    --
    Christopher Benson-Manica | I *should* know what I'm talking about - if I
    ataru(at)cyberspace.org | don't, I need to know. Flames welcome.
     
    Christopher Benson-Manica, Dec 13, 2005
    #1
    1. Advertising

  2. Christopher Benson-Manica

    Eric Sosman Guest

    Christopher Benson-Manica wrote On 12/13/05 12:39,:
    > Is it possible to write a macro (in unextended C89) such that
    >
    > TEST( int, (1,2,3) );
    >
    > expands to
    >
    > int array[]={ 1,2,3 };
    >
    > ? I strongly suspect that it is not, but I don't wish to overlook a
    > solution if one exists.


    #define TEST(type,list) type array[] = { list }

    This seems pretty straightforward -- have I somehow
    misread your question?

    --
     
    Eric Sosman, Dec 13, 2005
    #2
    1. Advertising

  3. Eric Sosman wrote:
    > Christopher Benson-Manica wrote On 12/13/05 12:39,:
    > > Is it possible to write a macro (in unextended C89) such that
    > >
    > > TEST( int, (1,2,3) );
    > >
    > > expands to
    > >
    > > int array[]={ 1,2,3 };
    > >
    > > ? I strongly suspect that it is not, but I don't wish to overlook a
    > > solution if one exists.

    >
    > #define TEST(type,list) type array[] = { list }
    >
    > This seems pretty straightforward -- have I somehow
    > misread your question?
    >
    > --
    >


    What about the parenthesis? I think he is trying to avoid C99 varargs
    macros.

    -David
     
    David Resnick, Dec 13, 2005
    #3
  4. David Resnick <> wrote:

    > What about the parenthesis? I think he is trying to avoid C99 varargs
    > macros.


    Yes, I am. I am dealing with a C++ implementation that does not
    support the C99 varargs macros (among other things).

    --
    Christopher Benson-Manica | I *should* know what I'm talking about - if I
    ataru(at)cyberspace.org | don't, I need to know. Flames welcome.
     
    Christopher Benson-Manica, Dec 13, 2005
    #4
  5. Christopher Benson-Manica

    Eric Sosman Guest

    David Resnick wrote On 12/13/05 13:14,:
    > Eric Sosman wrote:
    >
    >>Christopher Benson-Manica wrote On 12/13/05 12:39,:
    >>
    >>>Is it possible to write a macro (in unextended C89) such that
    >>>
    >>>TEST( int, (1,2,3) );
    >>>
    >>>expands to
    >>>
    >>>int array[]={ 1,2,3 };
    >>>
    >>>? I strongly suspect that it is not, but I don't wish to overlook a
    >>>solution if one exists.

    >>
    >> #define TEST(type,list) type array[] = { list }
    >>
    >>This seems pretty straightforward -- have I somehow
    >>misread your question?

    >
    >
    > What about the parenthesis? I think he is trying to avoid C99 varargs
    > macros.


    Aha: I *did* misread the question! Thanks, D R,
    and sorry, C B-M.

    --
     
    Eric Sosman, Dec 13, 2005
    #5
  6. Christopher Benson-Manica a écrit :
    > Is it possible to write a macro (in unextended C89) such that
    >
    > TEST( int, (1,2,3) );
    >
    > expands to
    >
    > int array[]={ 1,2,3 };
    >
    > ? I strongly suspect that it is not, but I don't wish to overlook a
    > solution if one exists.
    >

    The obvious way :

    #define TEST(T, a, b, c)\
    T array[] = {a, b, c}

    int main(void)
    {

    TEST (int, 1, 2, 3);

    return 0;
    }

    But I guess you want a variable list. Il suggest this :

    #include <stdio.h>

    int main(void)
    {

    int array[] =
    {
    #define ITEM(value)\
    value,
    #include "test.itm"
    #undef ITEM
    };

    size_t i;

    for (i = 0; i < sizeof array / sizeof *array; i++)
    {
    printf ("array[%u] = %d\n", (unsigned) i, array);
    }

    return 0;
    }

    with:

    /* test.itm */

    ITEM (12)
    ITEM (34)
    ITEM (56)

    which is a trick I got here on c.l.c a few years ago. I'm using it day
    and night to automate code generation ! Very powerful ! (Prefcet to give
    a string to an enum, for example...)

    --
    A+

    Emmanuel Delahaye
     
    Emmanuel Delahaye, Dec 13, 2005
    #6
  7. Eric Sosman a écrit :
    >
    > Christopher Benson-Manica wrote On 12/13/05 12:39,:
    >
    >>Is it possible to write a macro (in unextended C89) such that
    >>
    >>TEST( int, (1,2,3) );
    >>
    >>expands to
    >>
    >>int array[]={ 1,2,3 };

    > #define TEST(type,list) type array[] = { list }
    >
    > This seems pretty straightforward -- have I somehow
    > misread your question?


    AFAICT, it doesn't work as expected:

    #include <stdio.h>

    #define TEST(type, list) type array[] = { list }

    int main(void)
    {
    TEST( int, (1,2,3) );

    size_t i;

    for (i = 0; i < sizeof array / sizeof *array; i++)
    {
    printf ("array[%u] = %d\n", (unsigned) i, array);
    }

    return 0;
    }

    produces

    array[0] = 3

    Some comma operator trick, I guess...

    --
    A+

    Emmanuel Delahaye
     
    Emmanuel Delahaye, Dec 13, 2005
    #7
  8. Christopher Benson-Manica wrote:
    > Is it possible to write a macro (in unextended C89) such that
    >
    > TEST( int, (1,2,3) );
    >
    > expands to
    >
    > int array[]={ 1,2,3 };
    >
    > ? I strongly suspect that it is not, but I don't wish to overlook a
    > solution if one exists.


    No, there isn't. The closest thing you can do is something like the
    following which is quite ugly:

    #define TEST(x,y) x array[]={y}
    #define SEP ,
    TEST(int, 1 SEP 2 SEP 3);

    and I am not positive that even this is valid in C89.

    Robert Gamble
     
    Robert Gamble, Dec 13, 2005
    #8
  9. Christopher Benson-Manica a écrit :
    > Is it possible to write a macro (in unextended C89) such that
    >
    > TEST( int, (1,2,3) );
    >
    > expands to
    >
    > int array[]={ 1,2,3 };
    >
    > ? I strongly suspect that it is not, but I don't wish to overlook a
    > solution if one exists.
    >

    The obvious way :

    #define TEST(T, a, b, c)\
    T array[] = {a, b, c}

    int main(void)
    {

    TEST (int, 1, 2, 3);

    return 0;
    }

    But I guess you want a variable list. Il suggest this :

    #include <stdio.h>

    int main(void)
    {

    int array[] =
    {
    #define ITEM(value)\
    value,
    #include "test.itm"
    #undef ITEM
    };

    size_t i;

    for (i = 0; i < sizeof array / sizeof *array; i++)
    {
    printf ("array[%u] = %d\n", (unsigned) i, array);
    }

    return 0;
    }

    with:

    /* test.itm */

    ITEM (12)
    ITEM (34)
    ITEM (56)

    which is a trick I got here on c.l.c a few years ago. I'm using it day
    and night to automate code generation ! Very powerful ! (Perfect to give
    a string to an enum, for example...)

    --
    A+

    Emmanuel Delahaye
     
    Emmanuel Delahaye, Dec 13, 2005
    #9
  10. Christopher Benson-Manica wrote:
    > Is it possible to write a macro (in unextended C89) such that
    >
    > TEST( int, (1,2,3) );
    >
    > expands to
    >
    > int array[]={ 1,2,3 };
    >
    > ? I strongly suspect that it is not, but I don't wish to overlook a
    > solution if one exists.
    >
    > --
    > Christopher Benson-Manica | I *should* know what I'm talking about - if I
    > ataru(at)cyberspace.org | don't, I need to know. Flames welcome.


    I assume you reject the (clunky)

    #define TEST_1(type,val1) type array[]={val1}
    #define TEST_2(type,val1,val2) type array[]={val1,val2}
    ....

    -David
     
    David Resnick, Dec 13, 2005
    #10
  11. David Resnick <> wrote:

    > I assume you reject the (clunky)


    > #define TEST_1(type,val1) type array[]={val1}
    > #define TEST_2(type,val1,val2) type array[]={val1,val2}


    Yes :)

    --
    Christopher Benson-Manica | I *should* know what I'm talking about - if I
    ataru(at)cyberspace.org | don't, I need to know. Flames welcome.
     
    Christopher Benson-Manica, Dec 13, 2005
    #11
  12. Emmanuel Delahaye <> wrote:

    > But I guess you want a variable list. Il suggest this :


    (snip interesting technique)

    > which is a trick I got here on c.l.c a few years ago. I'm using it day
    > and night to automate code generation ! Very powerful ! (Perfect to give
    > a string to an enum, for example...)


    That does look like a creative technique, and I'll look for
    opportunities to use it. Unfortunately, it doesn't handle the actual
    situation I hav; I admit that what I want to do is not a stupendous
    idea. The ultimate idea was to allow use of "array literals" (such as
    are found in JavaScript) to allow for macros like

    if( TEST(int,1,(1,2,3)) ) { /* Checks for second argument in third */
    /* ... */
    }

    None of the other options (inline function, C++ templates, etc.) are
    particularly appetizing here either, so it seems that there is no
    trick to avoiding tried-and-true (but boring) methods.

    --
    Christopher Benson-Manica | I *should* know what I'm talking about - if I
    ataru(at)cyberspace.org | don't, I need to know. Flames welcome.
     
    Christopher Benson-Manica, Dec 13, 2005
    #12
  13. Robert Gamble <> wrote:

    > #define TEST(x,y) x array[]={y}
    > #define SEP ,
    > TEST(int, 1 SEP 2 SEP 3);


    > and I am not positive that even this is valid in C89.


    It seems to work on my implementation, FWIW, but I concur that it is
    probably too ugly to consider.

    --
    Christopher Benson-Manica | I *should* know what I'm talking about - if I
    ataru(at)cyberspace.org | don't, I need to know. Flames welcome.
     
    Christopher Benson-Manica, Dec 13, 2005
    #13
  14. Christopher Benson-Manica

    Ben Pfaff Guest

    Christopher Benson-Manica <> writes:

    > That does look like a creative technique, and I'll look for
    > opportunities to use it. Unfortunately, it doesn't handle the actual
    > situation I hav; I admit that what I want to do is not a stupendous
    > idea. The ultimate idea was to allow use of "array literals" (such as
    > are found in JavaScript) to allow for macros like
    >
    > if( TEST(int,1,(1,2,3)) ) { /* Checks for second argument in third */
    > /* ... */
    > }


    For small ranges, you can do something like this:
    #define BIT(x) (1ul << (x))
    #define BITS(x, y) (BIT(x) | (y))

    if (BIT(x) & BITS(1, BITS(2, BIT(3))))
    Not ideal.
    --
    "For those who want to translate C to Pascal, it may be that a lobotomy
    serves your needs better." --M. Ambuhl

    "Here are the steps to create a C-to-Turbo-Pascal translator..." --H. Schildt
     
    Ben Pfaff, Dec 13, 2005
    #14
  15. Christopher Benson-Manica

    Jordan Abel Guest

    On 2005-12-13, Christopher Benson-Manica <> wrote:
    > David Resnick <> wrote:
    >
    >> I assume you reject the (clunky)

    >
    >> #define TEST_1(type,val1) type array[]={val1}
    >> #define TEST_2(type,val1,val2) type array[]={val1,val2}

    >
    > Yes :)


    how about (c99 only)

    #define TEST(type,...) type array[] = { __VA_ARGS__ }
     
    Jordan Abel, Dec 13, 2005
    #15
  16. Jordan Abel wrote:
    > On 2005-12-13, Christopher Benson-Manica <> wrote:
    > > David Resnick <> wrote:
    > >
    > >> I assume you reject the (clunky)

    > >
    > >> #define TEST_1(type,val1) type array[]={val1}
    > >> #define TEST_2(type,val1,val2) type array[]={val1,val2}

    > >
    > > Yes :)

    >
    > how about (c99 only)
    >
    > #define TEST(type,...) type array[] = { __VA_ARGS__ }


    Apparently you missed the part about c99 not being an option, but yes,
    this would be the obvious solution if it were.

    Robert Gamble
     
    Robert Gamble, Dec 13, 2005
    #16
    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. Cronus
    Replies:
    1
    Views:
    717
    Paul Mensonides
    Jul 15, 2004
  2. noe
    Replies:
    25
    Views:
    674
    Leor Zolman
    May 28, 2004
  3. Sridhar R

    distutils possibilities

    Sridhar R, Feb 14, 2004, in forum: Python
    Replies:
    1
    Views:
    245
    Jorgen Grahn
    Feb 14, 2004
  4. Skip Montanaro
    Replies:
    1
    Views:
    351
    Torsten Mohr
    Jun 7, 2004
  5. Daniel Hilgarth
    Replies:
    1
    Views:
    570
    Martin Honnen
    Nov 25, 2006
Loading...

Share This Page