generate variable names

Discussion in 'C Programming' started by panthera, Jun 7, 2007.

  1. panthera

    panthera Guest

    Hi,

    I have a problem regarding generating variable names in my
    program.Suppose I have 10 variables b0,b1,b2...b9.They are NOT in an
    array.And I want to randomly select one of them and get its value.So I
    pick a random number between 0 and 9 and concat it with 'b' to get the
    name of this variable.But how do I get the value of this variable?

    I tried to define a macro using b##num,but it results in bnum instead
    of b5 for example.

    Thx

    panthera
     
    panthera, Jun 7, 2007
    #1
    1. Advertising

  2. panthera

    Eric Sosman Guest

    panthera wrote On 06/07/07 16:21,:
    > Hi,
    >
    > I have a problem regarding generating variable names in my
    > program.Suppose I have 10 variables b0,b1,b2...b9.They are NOT in an
    > array.And I want to randomly select one of them and get its value.So I
    > pick a random number between 0 and 9 and concat it with 'b' to get the
    > name of this variable.But how do I get the value of this variable?


    Use an array.

    Yes, I know you said they are "NOT in an array."
    That's a mistake; fix it.

    > I tried to define a macro using b##num,but it results in bnum instead
    > of b5 for example.


    Wrong tool for the job. (Also, a tool that you do
    not seem to understand yet.)

    --
     
    Eric Sosman, Jun 7, 2007
    #2
    1. Advertising

  3. "panthera" <> wrote in message
    news:...
    > Hi,
    >
    > I have a problem regarding generating variable names in my
    > program.Suppose I have 10 variables b0,b1,b2...b9.They are NOT in an
    > array.And I want to randomly select one of them and get its value.So I
    > pick a random number between 0 and 9 and concat it with 'b' to get the
    > name of this variable.But how do I get the value of this variable?


    Why don't you put the addresses of the variables into an array:

    int *addr[10];

    addr[0] = &b0;
    ....(etc)...

    Then you can reference their values as *addr;

    karl m
     
    Karl Malbrain, Jun 7, 2007
    #3
  4. panthera

    Default User Guest

    panthera wrote:

    > Hi,
    >
    > I have a problem regarding generating variable names in my
    > program.


    Variable names don't work that way, they aren't a run-time construct.
    They are used by the compiler. You'll have to find a way to map
    generated names at runtime to values or pointers stored in some manner.

    One way is to have two parallel arrays, one with int values (or
    pointers if you need to change the contents of the variables) and
    another with names. You then construct your random name, find a match
    in the name array, and access the variable value that has the same
    index.




    Brian
     
    Default User, Jun 7, 2007
    #4
  5. panthera

    Army1987 Guest

    "panthera" <> ha scritto nel messaggio
    news:...
    > Hi,
    >
    > I have a problem regarding generating variable names in my
    > program.Suppose I have 10 variables b0,b1,b2...b9.They are NOT in an
    > array.And I want to randomly select one of them and get its value.

    Why?

    So I
    > pick a random number between 0 and 9 and concat it with 'b' to get the
    > name of this variable.But how do I get the value of this variable?
    >
    > I tried to define a macro using b##num,but it results in bnum instead
    > of b5 for example.



    *(num == 0 ? &b0 : (num == 1 ? &b1 : (num == 2 ? &b2 : /*etc*/ )))
     
    Army1987, Jun 8, 2007
    #5
  6. Eric Sosman wrote:
    > panthera wrote On 06/07/07 16:21,:
    >> Hi,
    >>
    >> I have a problem regarding generating variable names in my
    >> program.Suppose I have 10 variables b0,b1,b2...b9.They are NOT in an
    >> array.And I want to randomly select one of them and get its value.So I
    >> pick a random number between 0 and 9 and concat it with 'b' to get the
    >> name of this variable.But how do I get the value of this variable?

    >
    > Use an array.
    >
    > Yes, I know you said they are "NOT in an array."
    > That's a mistake; fix it.
    >

    if you (for some to me very unlikely reason) can not use an array this
    is a possible solution:

    variable_type getValue(int number)
    {
    switch (number)
    {
    case 0: return b0;
    case 1: return b1;
    /* and so on*/
    case 9: return b9;
    }
    return 0; /* or whatever you would like as a fail-safe value*/
    }

    substitute variable_type to whatever type your variables have.

    But, as stated above: using an array is definitely preferable to this,
    it is just another possible solution!
    Also note that ten cases is somewhat reasonable, expanding the problem
    to 10000 cases is probably not.
     
    Johan Bengtsson, Jun 9, 2007
    #6
    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. Weng Tianxiang
    Replies:
    5
    Views:
    1,351
    Christophe
    Feb 16, 2006
  2. fBechmann
    Replies:
    0
    Views:
    417
    fBechmann
    Jun 10, 2004
  3. wanwan
    Replies:
    3
    Views:
    453
    Alex Martelli
    Oct 14, 2005
  4. Marcus Kwok
    Replies:
    6
    Views:
    479
    Phlip
    Apr 5, 2006
  5. News123
    Replies:
    2
    Views:
    481
    John Machin
    Nov 26, 2008
Loading...

Share This Page