loop control in macro?

This is a discussion on loop control in macro? within the C Programming forums, part of the General Programming Boards category; Hi, Gurus, Pure C, I am trying to define a enum type with variable number of elements, controlled by macro. ...

  1. #1
    Registered User
    Join Date
    May 2010
    Posts
    6

    Question loop control in macro?

    Hi, Gurus,

    Pure C, I am trying to define a enum type with variable number of elements, controlled by macro.

    for example

    Code:
    #define A 2
    
    typdef enum
    {
    
    A_0
    A_1
    } ENUM_TABLE
    is there a way to make this enum definition automatic populate bases on definition of "A"
    A_0, ...A_#(A-1),

    Great, thanks,

  2. #2
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,443
    By "pure" C, I assume you mean standard and portable, and I'm not aware of any way to do that. There is the __COUNTER__ preprocessing symbol but I don't think it's standard, and I don't know how you would stop it at A-1. I'm also not sure why you would want to something like that. The way I see it, if you have items for which a simple number is all you need to distinguish them, then I would use an array, where the name of the array reflects it's use/purpose (A in your example), and the index corresponds to the number in your example, and you just define A_MAX for the size of the array. If the values are distinguished by something more significant than a number, then it seems like appending a word would be best, such as A_FOO, A_BAR, which can't really be automated anyhow.

    Maybe if you could explain in more detail what specifically you're trying to do we could help you find a solution.

  3. #3
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    No, and I have no idea why you would want to do such a thing. Is it really that hard to add one more line to your enum table manually? I mean it's not like you can actually have your code automatically generate new case statements or something just because you increase the definition of A.


    Quzah.
    Hope is the first step on the road to disappointment.

  4. #4
    Registered User
    Join Date
    May 2010
    Posts
    6

    Red face

    Great thanks to all prompt comments,

    I am trying to do something like this: a enum type has "A" elements, naming of the elements doesn't really matter, reason to choose "A_#" is for possible loop control.

    number of elements in the enum type should be variable.
    One case:
    Code:
    #define A 100
    Another case:
    Code:
    #define A 1000
    If it's doable, I can have a code smart enough at precompile to generate variable size of the ENUM.

    I don't imaging variable switch case is possible. So didn't ask,

  5. #5
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,443
    Quote Originally Posted by legendbb View Post
    I am trying to do something like this: a enum type has "A" elements, naming of the elements doesn't really matter, reason to choose "A_#" is for possible loop control.
    There's no 'eval' function in C. You can't take some loop control variable, like i, and have it's value be turned into a symbol during run-time. You can only append the symbol name (i) to A, so every iteration of your loop will produce A_i regardless of i's value. You can never get A_3, A_42, etc. I think you're heading down the wrong path. Seriously, this sounds like a great case for using arrays.

  6. #6
    'Allo, 'Allo, Allo
    Join Date
    Apr 2008
    Posts
    611
    Boost's preprocessor library can do it. AFAIK it's solely implemented using the preprocessor which I don't think has changed significantly between C and C++. Of course, just because it (might) be possible doesn't mean it's a good idea.

  7. #7
    Registered User
    Join Date
    May 2010
    Posts
    6
    Great thanks to all comment, I give up this idea.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Program Control (for loop)
    By naspek in forum C Programming
    Replies: 3
    Last Post: 01-28-2010, 05:25 PM
  2. Control loop with scanf
    By sandeep156 in forum C Programming
    Replies: 5
    Last Post: 05-02-2009, 02:06 PM
  3. Region As Loop Control
    By SMurf in forum Windows Programming
    Replies: 2
    Last Post: 04-25-2007, 12:19 PM
  4. Getting the loop control form the user
    By Extropian in forum C Programming
    Replies: 6
    Last Post: 08-11-2005, 09:50 AM
  5. Loop control
    By astro_not in forum C Programming
    Replies: 5
    Last Post: 02-28-2003, 07:43 AM

Tags for this Thread


1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21