Thread: POINT structure initialization

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

    POINT structure initialization

    I don't get the values that I assigned. What's happening?

    Code:
    #include <Windows.h>
    #include <iostream>
    using namespace std;
    
    int main()
    {
        int xCoord=40, yCoord=140;
    
        POINT points[5] = {(xCoord, yCoord),
                           (xCoord, yCoord-50),
                           (xCoord+50, yCoord-50),
                           (xCoord+50, yCoord)
                          };
    
        for (i = 0 ; i < 4 ; i++)
        {
            cout << points[i].x << ", ";
            cout << points[i].y << endl;
        }
    
        return 0;
    }
    This is what I get:
    140, 90
    90, 140
    0, 0
    0, 0
    Using Windows 10 with Code Blocks and MingW.

  2. #2
    Registered User
    Join Date
    Jun 2011
    Posts
    4,513
    This didn't even compile, since there was an error. It also gave me a bunch of warnings:

    Code:
    /*
    main.cpp||In function 'int main()':|
    main.cpp|9|warning: left-hand operand of comma has no effect|
    main.cpp|10|warning: left-hand operand of comma has no effect|
    main.cpp|11|warning: left-hand operand of comma has no effect|
    main.cpp|12|warning: left-hand operand of comma has no effect|
    main.cpp|13|warning: missing braces around initializer for 'POINT'|
    main.cpp|13|warning: missing braces around initializer for 'POINT'|
    main.cpp|15|error: 'i' was not declared in this scope|
    ||=== Build finished: 1 errors, 6 warnings ===|
    */
    The "missing braces around the initializer" are the key to your problem. Use braces, not parenthesis:

    Code:
    POINT points[5] = {{xCoord, yCoord},
                       {xCoord, yCoord-50},
                       {xCoord+50, yCoord-50},
                       {xCoord+50, yCoord}
                      };

  3. #3
    Master Apprentice phantomotap's Avatar
    Join Date
    Jan 2008
    Posts
    5,108
    [Edit]Dirty Ninja's[/Edit]

    O_o

    Actually, you totally get the values you assign.

    Look around for information about the comma operator and nested initialization.

    [Edit]
    @Ducky: You can more easily find such simple mistakes by, as Matticus, turning your compiler warnings up.
    [/Edit]

    Soma

    Hint: You aren't assigning the values you think.
    Last edited by phantomotap; 09-17-2014 at 06:29 AM.
    “Salem Was Wrong!” -- Pedant Necromancer
    “Four isn't random!” -- Gibbering Mouther

  4. #4
    Registered User
    Join Date
    Oct 2006
    Posts
    3,445
    Quote Originally Posted by phantomotap View Post
    [Edit]Dirty Ninja's[/Edit]
    Dirty ninja?
    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?

  5. #5
    Registered User
    Join Date
    Dec 2007
    Posts
    932
    Thank you!

    That was it and I will turn on my warnings up.
    Last edited by Ducky; 09-17-2014 at 07:05 AM.
    Using Windows 10 with Code Blocks and MingW.

  6. #6
    Master Apprentice phantomotap's Avatar
    Join Date
    Jan 2008
    Posts
    5,108
    Dirty ninja?
    O_o

    The post by Matticus wasn't here when I started my post. = Ninja'd

    Soma
    “Salem Was Wrong!” -- Pedant Necromancer
    “Four isn't random!” -- Gibbering Mouther

  7. #7
    Registered User
    Join Date
    Jun 2011
    Posts
    4,513
    [Edit]Dirty Ninja's[/Edit]
    My shinobi shōzoku is at the cleaners ... I swear!

  8. #8
    Registered User
    Join Date
    Oct 2006
    Posts
    3,445
    Look up the urban dictionary definition of dirty ninja.
    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
    Master Apprentice phantomotap's Avatar
    Join Date
    Jan 2008
    Posts
    5,108
    Look up the urban dictionary definition of dirty ninja.
    [Edit]
    ^_^

    Nevermind. After thinking about it, that definition works even better as an expletive.
    [/Edit]

    Soma
    Last edited by phantomotap; 09-17-2014 at 08:07 AM.
    “Salem Was Wrong!” -- Pedant Necromancer
    “Four isn't random!” -- Gibbering Mouther

  10. #10
    Registered User
    Join Date
    Oct 2006
    Posts
    3,445
    Quote Originally Posted by phantomotap View Post
    [Edit]
    ^_^

    Nevermind. After thinking about it, that definition works even better as an expletive.
    [/Edit]

    Soma
    The one that had me in tears laughing was in the example sentence, where they mentioned the "alaskan pipeline." Of course I had to find out what an alaskan pipeline is...
    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 antred's Avatar
    Join Date
    Apr 2012
    Location
    Germany
    Posts
    257
    Quote Originally Posted by Elkvis View Post
    Look up the urban dictionary definition of dirty ninja.
    Holy crap, now I wish I hadn't.

  12. #12
    Registered User Alpo's Avatar
    Join Date
    Apr 2014
    Posts
    877
    I nearly barfed reading that. Quick someone give me a lobotomy!
    WndProc = (2[b] || !(2[b])) ? SufferNobly : TakeArms;

  13. #13
    Registered User MutantJohn's Avatar
    Join Date
    Feb 2013
    Posts
    2,665
    I lol'd.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Structure initialization
    By krkr in forum C Programming
    Replies: 3
    Last Post: 05-30-2014, 04:40 AM
  2. point to a structure member
    By behzad_shabani in forum C Programming
    Replies: 5
    Last Post: 06-12-2008, 03:59 AM
  3. designated structure initialization not allowed in c++?
    By Sebastiani in forum C++ Programming
    Replies: 2
    Last Post: 09-15-2007, 09:18 AM
  4. Replies: 9
    Last Post: 10-26-2005, 07:29 AM
  5. structure member initialization
    By ivandn in forum C Programming
    Replies: 4
    Last Post: 10-27-2001, 01:27 PM