Thread: compile time error in example program

  1. #1
    Registered User
    Join Date
    Aug 2005
    Posts
    6

    compile time error in example program

    Hello,

    I was trying to compile an example Windows program from the main web site (code duplicated below) in my Bloodshed Dev-C++ compiler, and I got the following error:

    Code:
    8 C:\Dev-Cpp\winDemo.cpp [Warning] passing NULL used for non-pointer converting 4 of `int MessageBoxA(HWND__*, const CHAR*, const CHAR*, UINT)'
    Here's the example program:

    Code:
    #include <windows.h>
    int APIENTRY WinMain(HINSTANCE hInstance,
                         HINSTANCE hPrevInstance,
                         LPSTR     lpCmdLine,
                         int       nCmdShow)
    {
     	
    	MessageBox(NULL, "\tHello World!", "My first windows app", NULL);
    	return 0;
    }
    The debugger found the error in the line starting with MessageBox. Does anyone have any ideas? Thanks

  2. #2
    int x = *((int *) NULL); Cactus_Hugger's Avatar
    Join Date
    Jul 2003
    Location
    Banks of the River Styx
    Posts
    902
    That last argument to MessageBox is a integer, not a pointer, so you'd pass 0 instead of NULL.

    At any rate, passing 0 might be a bad idea. The last argument sets the icon and buttons of the message box, and I can't remember if 0 puts no buttons or if it defaults to something. I usually always pass something, I'd choose MB_OK in your case.

    Same for the first argument... I usually pass something there too, though NULL is ok. Instead of NULL, I usually give HWND_DESKTOP. (Equivalent in the long run, I think)
    Code:
    MessageBox(HWND_DESKTOP, "This text appears in the box.", "This is the title or caption text.", MB_OK);
    See MessageBox.
    Last edited by Cactus_Hugger; 12-31-2005 at 11:38 PM.
    long time; /* know C? */
    Unprecedented performance: Nothing ever ran this slow before.
    Any sufficiently advanced bug is indistinguishable from a feature.
    Real Programmers confuse Halloween and Christmas, because dec 25 == oct 31.
    The best way to accelerate an IBM is at 9.8 m/s/s.
    recursion (re - cur' - zhun) n. 1. (see recursion)

  3. #3
    Registered User
    Join Date
    Mar 2005
    Location
    Juneda
    Posts
    291
    I think that there's no problem with the window handler, insetad of that (Cactus_Hugger has posted the corrected code) the problem is on the last parameter, you have to use a valid value or you should specify that 'NULL' is an UINT
    Code:
    MessageBox(NULL,"Hey","",(UINT)NULL);
    niara

  4. #4
    int x = *((int *) NULL); Cactus_Hugger's Avatar
    Join Date
    Jul 2003
    Location
    Banks of the River Styx
    Posts
    902
    You should not pass NULL for the last argument of MessageBox. Perhaps I was not clear: The last argument of MessageBox() is UINT uType. Unsigned integer, a number. So if you don't want to pass anything, use 0, not NULL. NULL should be used for pointers, and the casting of NULL to UINT should tell you somethings up.
    Code:
    MessageBox(NULL, "Hey", "", 0);
    Read the FAQ article on why NULL and 0 are not the same.

    Edit: Looked it up. Turns out MB_OK is #defined as 0. So passing 0 and MB_OK are the same, but the latter is clearer in meaning. (Hence my original example.)
    long time; /* know C? */
    Unprecedented performance: Nothing ever ran this slow before.
    Any sufficiently advanced bug is indistinguishable from a feature.
    Real Programmers confuse Halloween and Christmas, because dec 25 == oct 31.
    The best way to accelerate an IBM is at 9.8 m/s/s.
    recursion (re - cur' - zhun) n. 1. (see recursion)

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. How to restrict a program in time consumed
    By gogoc in forum C++ Programming
    Replies: 3
    Last Post: 05-19-2008, 11:10 AM
  2. Finding Program Running Time
    By pf732k3 in forum C Programming
    Replies: 6
    Last Post: 03-18-2008, 01:56 PM
  3. Replies: 11
    Last Post: 10-24-2004, 10:28 AM
  4. The Timing is incorret
    By Drew in forum C++ Programming
    Replies: 5
    Last Post: 08-28-2003, 04:57 PM