Thread: #define array[8]

  1. #1
    Registered User
    Join Date
    Dec 2007
    Posts
    932

    #define array[8]

    Hi,

    I need to create a chessboard with 64 squares.
    Is it possible to define an initialize an array with #define?

    Code:
    #define ID_SQUARE[64]  for (int i=1; i<65; i++) ID_SQUARE[i]
    Last edited by Ducky; 05-17-2013 at 06:10 AM.
    Using Windows 10 with Code Blocks and MingW.

  2. #2
    Registered User
    Join Date
    Oct 2006
    Posts
    3,445
    your syntax is a bit off, but you can definitely do things like that. in C++ it's completely unnecessary though, because you have things like std::vector, which can be initialized to a specific size and populate all elements with a particular value. C-style arrays are effectively obsolete.
    What can this strange device be?
    When I touch it, it gives forth a sound
    It's got wires that vibrate and give music
    What can this thing be that I found?

  3. #3
    Registered User
    Join Date
    Jun 2005
    Posts
    6,815
    No.

    How about explain WHAT you're trying to achieve - in english, in the way a chess teacher might explain it to a child, rather than to a programmer - rather than making random guesses about how to achieve it in C++?
    Right 98% of the time, and don't care about the other 3%.

    If I seem grumpy or unhelpful in reply to you, or tell you you need to demonstrate more effort before you can expect help, it is likely you deserve it. Suck it up, Buttercup, and read this, this, and this before posting again.

  4. #4
    Registered User
    Join Date
    Dec 2007
    Posts
    932
    Thanks.
    These values go in a Win32 function as HMENU parameters and the compiler asking me for a const value so I dont think a C++ vector will do.
    Using Windows 10 with Code Blocks and MingW.

  5. #5
    Registered User
    Join Date
    Aug 2010
    Location
    Poland
    Posts
    733
    Quote Originally Posted by Ducky View Post
    Thanks.
    These values go in a Win32 function as HMENU parameters and the compiler asking me for a const value so I dont think a C++ vector will do.
    "Will do".

    However, not only it is usually a bad idea to go with bare Win32 API, it is also dangerous to mix C-style libraries with C++ (e.g., memory management), and even more dangerous if it is done by a beginner.

  6. #6
    Registered User
    Join Date
    Oct 2006
    Posts
    3,445
    Quote Originally Posted by Ducky View Post
    Thanks.
    These values go in a Win32 function as HMENU parameters and the compiler asking me for a const value so I dont think a C++ vector will do.
    that's exactly why std::vector has data() member function. it returns a pointer to the beginning of the internal array. you can then also use the size() member function to get the number of elements in the vector.
    What can this strange device be?
    When I touch it, it gives forth a sound
    It's got wires that vibrate and give music
    What can this thing be that I found?

  7. #7
    Registered User
    Join Date
    Dec 2007
    Posts
    932
    I tried to use a vector and I can initialize it but when I try to use it in WM_COMMAND it wont compile:

    error C2051: case expression not constant|

    Code:
    vector<int> ID_SQUARE;
            for (int i=1; i<65; i++)
              ID_SQUARE.push_back(i);
    Later...
    WM_COMMAND
    case ID_SQUARE[1]
    :
    Using Windows 10 with Code Blocks and MingW.

  8. #8
    Registered User
    Join Date
    Oct 2006
    Posts
    3,445
    Quote Originally Posted by Ducky View Post
    Code:
    vector<int> ID_SQUARE;
            for (int i=1; i<65; i++)
              ID_SQUARE.push_back(i);
    Later...
    WM_COMMAND
    case ID_SQUARE[1]
    :
    you can't use non-constant values in a case statement. this will be the case whether you use an array or an std::vector.
    What can this strange device be?
    When I touch it, it gives forth a sound
    It's got wires that vibrate and give music
    What can this thing be that I found?

  9. #9
    Registered User
    Join Date
    Dec 2007
    Posts
    932
    Thanks.
    So no other choice but to define 64 values unless I try to use 'if else' statements.
    Last edited by Ducky; 05-17-2013 at 09:37 AM.
    Using Windows 10 with Code Blocks and MingW.

  10. #10
    Registered User
    Join Date
    Oct 2006
    Posts
    3,445
    perhaps we can find a better solution if you explain your problem and what you're trying to do in a little more detail.
    What can this strange device be?
    When I touch it, it gives forth a sound
    It's got wires that vibrate and give music
    What can this thing be that I found?

  11. #11
    Registered User
    Join Date
    Apr 2013
    Posts
    1,658
    Quote Originally Posted by Elkvis View Post
    perhaps we can find a better solution if you explain your problem and what you're trying to do in a little more detail.
    He's stuck with constant values passed to the windows program as messages. He mentions HMENU, but if the squares are laid out on the screen, then he can optionally get messages as the mouse pointer moves through the matrix of squares, and/or get a message if the user clicks on a square, and windows will identify the squares through the resource with constant identifiers.

    A program could be used to create the include file(s) for the squares.

  12. #12
    Registered User
    Join Date
    Dec 2007
    Posts
    932
    Yes thats what Im trying to do and I ended up creating the 64 #defines with a program indeed.
    Mouse pointer would be a good idea too thats right but I think creating the chess cases as buttons is easier.
    Using Windows 10 with Code Blocks and MingW.

  13. #13
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by Ducky
    Yes thats what Im trying to do and I ended up creating the 64 #defines with a program indeed.
    It seems to me that an enumeration fits the bill.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  14. #14
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,612
    You could make the squares an enum.

  15. #15
    Registered User
    Join Date
    Dec 2007
    Posts
    932
    An enumeration, good idea, thanks.
    Using Windows 10 with Code Blocks and MingW.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Define a macro to sum the elements of an array
    By volte in forum C Programming
    Replies: 6
    Last Post: 07-22-2011, 04:22 AM
  2. Define an array/vector
    By kliro89 in forum C Programming
    Replies: 33
    Last Post: 01-27-2011, 01:59 AM
  3. #define array[]
    By cs05pp2 in forum C Programming
    Replies: 11
    Last Post: 10-31-2009, 01:26 PM
  4. define array size at later stage of program?
    By DavidDobson in forum C Programming
    Replies: 2
    Last Post: 05-04-2008, 03:35 PM
  5. [Prob] Can't define array String
    By Dark_Ruler in forum C++ Programming
    Replies: 4
    Last Post: 03-07-2006, 05:43 PM