Thread: Combining words to make variable name?

  1. #1
    Registered User
    Join Date
    Mar 2005
    Location
    Freeport, IL
    Posts
    32

    Question Combining words to make variable name?

    I'm just wondering if this is possible, hopefully I can explain what i'm trying to find out.

    say I have a game where the 2 players are represented by 2 different arrays, called arPlayer1 and arPlayer2.

    Now I have an area that will do something on the arrays. Now instead of putting like an if statement to change between arPlayer1 or arPlayer2 is there a way to do something like concatonate things to equal the array variable?

    like arPlayer + playernumber[2]

    and if playernumber is equal to 1, the array used would be arPlayer2.

    Just curious. I actually just realized it will work better if I use a function. But still would like to know if this can be done.

    Thanks

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    It's called "arrays". You shouldn't have arPlayer1 and arPlayer2, you should have arPlayer[0] and arPlayer[1]. Then you can just use arPlayer[playernumber] and be done with it.

  3. #3
    Registered User
    Join Date
    Mar 2005
    Location
    Freeport, IL
    Posts
    32
    I understand that, but in the program each player is using an array of 7 elements.

  4. #4
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by Djanvk View Post
    I understand that, but in the program each player is using an array of 7 elements.
    So you need an array of arrays [or usually better, an array of structs which has 7 elements].

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  5. #5
    Registered User
    Join Date
    Mar 2005
    Location
    Freeport, IL
    Posts
    32
    I've started the array of array, but I'll have to look into structs.

    Thanks

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Seg Fault in Compare Function
    By tytelizgal in forum C Programming
    Replies: 1
    Last Post: 10-25-2008, 03:06 PM
  2. seg fault at vectornew
    By tytelizgal in forum C Programming
    Replies: 2
    Last Post: 10-25-2008, 01:22 PM
  3. To use words for a variable
    By Saimadhav in forum C++ Programming
    Replies: 7
    Last Post: 07-09-2008, 03:37 PM
  4. Replies: 5
    Last Post: 01-13-2006, 12:00 AM
  5. pointers
    By InvariantLoop in forum C Programming
    Replies: 13
    Last Post: 02-04-2005, 09:32 AM