Thread: Help on understanding a piece of code

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

    Help on understanding a piece of code

    I cant interpret this snippet of code.
    I suspect its
    if GetLastError() returns ERROR_SUCCESS then assign something to bRet
    But where is the if() statement?
    Code:
    BOOL bRet=FALSE;
    bRet=(GetLastError() == ERROR_SUCCESS);
    Using Windows 10 with Code Blocks and MingW.

  2. #2
    -bleh-
    Join Date
    Aug 2010
    Location
    somewhere in this universe
    Posts
    463
    Can't tell without knowing how BOOL is defined. The return of "==" maybe automatically converted to BOOL. This can be risky. It's best to use C++ type "bool". Best guess is that BOOL is defined with int 0 and 1 or unsigned 0 and 1. Then C++'s bool true/false returned by "==" is converted to 0/1.
    Last edited by nimitzhunter; 05-11-2011 at 12:56 PM.
    "All that we see or seem
    Is but a dream within a dream." - Poe

  3. #3
    Registered User
    Join Date
    Aug 2010
    Location
    Poland
    Posts
    733
    BOOL is defined as an integral type and everybody who has ever touched WinAPI knows this. This isn't risky, it's good to stay with WinAPI types when writing Win code.
    bRet is assigned to the result of expression (GetLastError() == ERROR_SUCCESS). So, if the condition is true (read: error code equals 0) the return value is true (read: success), otherwise it is false (read: failure).
    Last edited by kmdv; 05-11-2011 at 01:30 PM.

  4. #4
    Registered User
    Join Date
    Dec 2007
    Posts
    932
    Thank you for the explanation!

    So it can be used instead of if() statement.
    Using Windows 10 with Code Blocks and MingW.

  5. #5
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by Ducky View Post
    Thank you for the explanation!

    So it can be used instead of if() statement.
    Only in cases where you want a true/false value in a variable... it can't make the decision if() makes...

  6. #6
    Gawking at stupidity
    Join Date
    Jul 2004
    Location
    Oregon, USA
    Posts
    3,218
    Think of it with a different kind of operation:
    Code:
    int a = 5 + 7;
    The "5 + 7" is evaluated and assigns the result (12) to "a".

    The == operator works the same way. It takes two operands, compares their equality, and evaluates to either 0 (false) or 1 (true):
    Code:
    int a = 5 == 7;
    5 does not equal 7, so "a" is assigned the value 0 (false).
    If you understand what you're doing, you're not learning anything.

  7. #7
    Registered User
    Join Date
    Dec 2007
    Posts
    932
    Yes, thats right, i understand now, great, thank you!
    Using Windows 10 with Code Blocks and MingW.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. K&R2 - Piece of code not working.
    By reqonekt in forum C Programming
    Replies: 2
    Last Post: 11-13-2009, 11:07 AM
  2. what's wrong with this piece of code
    By studentc in forum C++ Programming
    Replies: 4
    Last Post: 01-11-2006, 07:58 PM
  3. Help with a piece of code
    By Victor4015 in forum C++ Programming
    Replies: 1
    Last Post: 11-16-2005, 05:38 PM
  4. Help with a little piece of code
    By cdonlan in forum C Programming
    Replies: 5
    Last Post: 11-15-2004, 12:38 PM
  5. What is your favorite piece of code?
    By Yoshi in forum A Brief History of Cprogramming.com
    Replies: 3
    Last Post: 01-22-2002, 07:12 AM