Can I do this in C? (another macro question)

Discussion in 'C Programming' started by Ann O'Nymous, May 14, 2010.

  1. Ann O'Nymous

    Ann O'Nymous Guest

    I have a struct definition like this:

    struct xxx {
    int val;
    char name[12];
    int count;
    };

    and I want to create instances of this with a macro, where the name of
    the struct instance is the first paramater appended with the string
    "_A", the first field is the first parameter (defined elsewhere), the
    second field is the character string (up to 11 chars + terminating null)
    and the third field is the second parameter.

    Something like this:

    #define foo(name,f) struct xxx name_A = {name,"name",f};

    but that's not quite correct. What I want to do is this:

    foo(cat,1) expands to: struct xxx cat_A = {cat,"cat",1};
    foo(dog,2) expands to: struct xxx dog_A = {dog,"dog",2};
    foo(horse,9) expands to: struct xxx horse_A = {horse,"horse",9};

    So, my questions are:
    How can I append a string "_A" to a parameter to make a name for
    the struct instance? Right now, it names every one literally "name_A".

    How can I get C to substitute the first parameter between the quote
    marks? Right now it substitutes the string "name" each time with
    no substitution.

    Actually, what I'd also really like is to also find a way to convert the
    string passed to upper case before substitution. I'd really like
    foo(sheep,6) to expand to: struct xxx sheep_A = {sheep,"SHEEP",6};
    Ann O'Nymous, May 14, 2010
    #1
    1. Advertising

  2. Ann O'Nymous

    Ian Collins Guest

    On 05/14/10 02:35 PM, Ann O'Nymous wrote:
    > I have a struct definition like this:
    >
    > struct xxx {
    > int val;
    > char name[12];
    > int count;
    > };
    >
    > and I want to create instances of this with a macro, where the name of
    > the struct instance is the first paramater appended with the string
    > "_A", the first field is the first parameter (defined elsewhere), the
    > second field is the character string (up to 11 chars + terminating null)
    > and the third field is the second parameter.
    >
    > Something like this:
    >
    > #define foo(name,f) struct xxx name_A = {name,"name",f};
    >
    > but that's not quite correct. What I want to do is this:
    >
    > foo(cat,1) expands to: struct xxx cat_A = {cat,"cat",1};
    > foo(dog,2) expands to: struct xxx dog_A = {dog,"dog",2};
    > foo(horse,9) expands to: struct xxx horse_A = {horse,"horse",9};


    Try

    #define foo( name,f ) struct xxx name##_A = { name, #name, f }

    Look up the preprocessor # and ## operators.

    > Actually, what I'd also really like is to also find a way to convert the
    > string passed to upper case before substitution. I'd really like
    > foo(sheep,6) to expand to: struct xxx sheep_A = {sheep,"SHEEP",6};


    Now that's just asking too much!

    --
    Ian Collins
    Ian Collins, May 14, 2010
    #2
    1. Advertising

  3. Ann O'Nymous

    Ann O'Nymous Guest

    On 5/13/2010 10:44 PM, Ian Collins wrote:

    > Try
    >
    > #define foo( name,f ) struct xxx name##_A = { name, #name, f }
    >
    > Look up the preprocessor # and ## operators.


    Cool! Thanks!

    >> Actually, what I'd also really like is to also find a way to convert the
    >> string passed to upper case before substitution. I'd really like
    >> foo(sheep,6) to expand to: struct xxx sheep_A = {sheep,"SHEEP",6};

    >
    > Now that's just asking too much!


    That's what I figured... :)
    Ann O'Nymous, May 14, 2010
    #3
    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. Dead RAM
    Replies:
    20
    Views:
    1,107
    John Harrison
    Jul 14, 2004
  2. D Senthil Kumar

    macro name from macro?

    D Senthil Kumar, Sep 20, 2003, in forum: C Programming
    Replies:
    1
    Views:
    576
    Jack Klein
    Sep 21, 2003
  3. sounak

    to get macro name from macro value

    sounak, Nov 22, 2005, in forum: C Programming
    Replies:
    17
    Views:
    500
    Mark McIntyre
    Nov 22, 2005
  4. Patrick Kowalzick
    Replies:
    5
    Views:
    469
    Patrick Kowalzick
    Mar 14, 2006
  5. Mike Manilone

    macro inside macro

    Mike Manilone, Oct 3, 2011, in forum: C Programming
    Replies:
    8
    Views:
    457
    Mike Manilone
    Oct 6, 2011
Loading...

Share This Page