Like Tree6Likes
  • 2 Post By Elkvis
  • 1 Post By Elkvis
  • 1 Post By rcgldr
  • 1 Post By laserlight
  • 1 Post By whiteflags

#define array[8]

This is a discussion on #define array[8] within the C++ Programming forums, part of the General Programming Boards category; Hi, I need to create a chessboard with 64 squares. Is it possible to define an initialize an array with ...

  1. #1
    Registered User
    Join Date
    Dec 2007
    Location
    France
    Posts
    747

    #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.
    Compiler MSVC++ 2010 with Code::Blocks.

  2. #2
    Registered User
    Join Date
    Oct 2006
    Posts
    2,144
    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.
    Ducky and Elysia like this.
    Code:
    namespace life
    {
        const bool change = true;
    }

  3. #3
    Registered User
    Join Date
    Jun 2005
    Posts
    5,864
    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%.

  4. #4
    Registered User
    Join Date
    Dec 2007
    Location
    France
    Posts
    747
    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.
    Compiler MSVC++ 2010 with Code::Blocks.

  5. #5
    Registered User
    Join Date
    Aug 2010
    Location
    Poland
    Posts
    671
    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.
    I never put signature, but I decided to make an exception.

  6. #6
    Registered User
    Join Date
    Oct 2006
    Posts
    2,144
    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.
    Code:
    namespace life
    {
        const bool change = true;
    }

  7. #7
    Registered User
    Join Date
    Dec 2007
    Location
    France
    Posts
    747
    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]
    :
    Compiler MSVC++ 2010 with Code::Blocks.

  8. #8
    Registered User
    Join Date
    Oct 2006
    Posts
    2,144
    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.
    Code:
    namespace life
    {
        const bool change = true;
    }

  9. #9
    Registered User
    Join Date
    Dec 2007
    Location
    France
    Posts
    747
    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.
    Compiler MSVC++ 2010 with Code::Blocks.

  10. #10
    Registered User
    Join Date
    Oct 2006
    Posts
    2,144
    perhaps we can find a better solution if you explain your problem and what you're trying to do in a little more detail.
    Ducky likes this.
    Code:
    namespace life
    {
        const bool change = true;
    }

  11. #11
    Registered User
    Join Date
    Apr 2013
    Posts
    1,169
    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.
    Ducky likes this.

  12. #12
    Registered User
    Join Date
    Dec 2007
    Location
    France
    Posts
    747
    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.
    Compiler MSVC++ 2010 with Code::Blocks.

  13. #13
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    20,975
    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.
    Ducky likes this.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  14. #14
    Registered User whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    7,527
    You could make the squares an enum.
    Ducky likes this.

  15. #15
    Registered User
    Join Date
    Dec 2007
    Location
    France
    Posts
    747
    An enumeration, good idea, thanks.
    Compiler MSVC++ 2010 with Code::Blocks.

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, 12: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, 04:43 PM

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