Okay, I finally understand that they point to memory, because I knew that all along.

I just wasn't aware of why they needed to point to memory for.

So I need to use pointers in this position but I don't know where to use them or on which things and the error messages from compiling don't make any sense at all.

Here's my finished code, but it won't compile:

/**************************
 * Includes
 *
 **************************/

#include <windows.h>
#include <gl/gl.h>


/**************************
 * Function Declarations
 *
 **************************/

LRESULT CALLBACK WndProc (HWND hWnd, UINT message,
WPARAM wParam, LPARAM lParam);
void EnableOpenGL (HWND *hWnd, HDC *hDC, HGLRC *hRC);
void DisableOpenGL (HWND *hWnd, HDC hDC, HGLRC hRC);


/**************************
 * WinMain
 *
 **************************/

int WINAPI WinMain (HINSTANCE hInstance,
                    HINSTANCE hPrevInstance,
                    LPSTR lpCmdLine,
                    int iCmdShow)
{
    WNDCLASS wc;
    HWND hWnd;
    HDC hDC;
    HGLRC hRC;        
    MSG msg;
    BOOL bQuit = FALSE;
    float theta = 0.0f;

    /* register window class */
    wc.style = CS_OWNDC;
    wc.lpfnWndProc = WndProc;
    wc.cbClsExtra = 0;
    wc.cbWndExtra = 0;
    wc.hInstance = hInstance;
    wc.hIcon = LoadIcon (NULL, IDI_APPLICATION);
    wc.hCursor = LoadCursor (NULL, IDC_ARROW);
    wc.hbrBackground = (HBRUSH) GetStockObject (BLACK_BRUSH);
    wc.lpszMenuName = NULL;
    wc.lpszClassName = "GLSample";
    RegisterClass (&wc);

    /* create main window */
    hWnd = CreateWindow (
      "GLSample", "OpenGL Sample", 
      WS_CAPTION | WS_POPUPWINDOW | WS_VISIBLE,
      0, 0, 256, 256,
      NULL, NULL, hInstance, NULL);

    /* enable OpenGL for the window */
    EnableOpenGL (hWnd, &hDC, &hRC);

    /* program main loop */
    while (!bQuit)
    {
        /* check for messages */
        if (PeekMessage (msg, NULL, 0, 0, PM_REMOVE))
        {
            /* handle or dispatch messages */
            if (msg.message == WM_QUIT)
            {
                bQuit = TRUE;
            }
            else
            {
                TranslateMessage (msg);
                DispatchMessage (msg);
            }
        }
        else
        {
            /* OpenGL animation code goes here */

            glClearColor (0.0f, 0.0f, 0.0f, 0.0f);
            glClear (GL_COLOR_BUFFER_BIT);

            glPushMatrix ();
            glRotatef (theta, 0.0f, 0.0f, 1.0f);
            glBegin (GL_TRIANGLES);
            glColor3f (1.0f, 0.0f, 0.0f);   glVertex2f (0.0f, 1.0f);
            glColor3f (0.0f, 1.0f, 0.0f);   glVertex2f (0.87f, -0.5f);
            glColor3f (0.0f, 0.0f, 1.0f);   glVertex2f (-0.87f, -0.5f);
            glEnd ();
            glPopMatrix ();

            SwapBuffers (hDC);

            theta += 1.0f;
            Sleep (1);
        }
    }

    /* shutdown OpenGL */
    DisableOpenGL (hWnd, hDC, hRC);

    /* destroy the window explicitly */
    DestroyWindow (hWnd);

    return msg.wParam;
}


/********************
 * Window Procedure
 *
 ********************/

LRESULT CALLBACK WndProc (HWND hWnd, UINT message,
                          WPARAM wParam, LPARAM lParam)
{

    switch (message)
    {
    case WM_CREATE:
        return 0;
    case WM_CLOSE:
        PostQuitMessage (0);
        return 0;

    case WM_DESTROY:
        return 0;

    case WM_KEYDOWN:
        switch (wParam)
        {
        case VK_ESCAPE:
            PostQuitMessage(0);
            return 0;
        }
        return 0;

    default:
        return DefWindowProc (hWnd, message, wParam, lParam);
    }
}


/*******************
 * Enable OpenGL
 *
 *******************/

void EnableOpenGL (HWND hWnd, HDC hDC, HGLRC hRC)
{
    PIXELFORMATDESCRIPTOR pfd;
    int iFormat;

    /* get the device context (DC) */
    *hDC = GetDC (hWnd);

    /* set the pixel format for the DC */
    ZeroMemory (&pfd, sizeof (pfd));
    pfd.nSize = sizeof (pfd);
    pfd.nVersion = 1;
    pfd.dwFlags = PFD_DRAW_TO_WINDOW | 
      PFD_SUPPORT_OPENGL | PFD_DOUBLEBUFFER;
    pfd.iPixelType = PFD_TYPE_RGBA;
    pfd.cColorBits = 24;
    pfd.cDepthBits = 16;
    pfd.iLayerType = PFD_MAIN_PLANE;
    iFormat = ChoosePixelFormat (*hDC, &pfd);
    SetPixelFormat (*hDC, iFormat, &pfd);

    /* create and enable the render context (RC) */
    *hRC = wglCreateContext( *hDC );
    wglMakeCurrent( *hDC, *hRC );

}


/******************
 * Disable OpenGL
 *
 ******************/

void DisableOpenGL (HWND hWnd, HDC hDC, HGLRC hRC)
{
    wglMakeCurrent (NULL, NULL);
    wglDeleteContext (hRC);
    ReleaseDC (hWnd, hDC);
}

The error says it can not convert HWND to HWND for one of the functions. What should I do? And what changes should I change to avoid making the mistake again?

It also says "CANNOT CONVERT MSG TO tagMSG for void function."

I have no idea what to do and tried tweaking around and it didn't help. Please some tips.

I just tweaked around with it a little more and now it says the following:

no match for 'operator=' in '*hDC = GetDC[hWnd]'
candidates are: HDC_& HDC_::operator=[const HDC_&]
cannot convert 'HDC_'to 'HDC_' for argument '1' to "int ChoosePixelFormat

And 9 more errors....

Recommended Answers

All 14 Replies

Okay, I finally understand that they point to memory, because I knew that all along.

I just wasn't aware of why they needed to point to memory for.

So I need to use pointers in this position but I don't know where to use them or on which things and the error messages from compiling don't make any sense at all.

Here's my finished code, but it won't compile:

/**************************
 * Includes
 *
 **************************/

#include <windows.h>
#include <gl/gl.h>


/**************************
 * Function Declarations
 *
 **************************/

LRESULT CALLBACK WndProc (HWND hWnd, UINT message,
WPARAM wParam, LPARAM lParam);
void EnableOpenGL (HWND *hWnd, HDC *hDC, HGLRC *hRC);
void DisableOpenGL (HWND *hWnd, HDC hDC, HGLRC hRC);


/**************************
 * WinMain
 *
 **************************/

int WINAPI WinMain (HINSTANCE hInstance,
                    HINSTANCE hPrevInstance,
                    LPSTR lpCmdLine,
                    int iCmdShow)
{
    WNDCLASS wc;
    HWND hWnd;
    HDC hDC;
    HGLRC hRC;        
    MSG msg;
    BOOL bQuit = FALSE;
    float theta = 0.0f;

    /* register window class */
    wc.style = CS_OWNDC;
    wc.lpfnWndProc = WndProc;
    wc.cbClsExtra = 0;
    wc.cbWndExtra = 0;
    wc.hInstance = hInstance;
    wc.hIcon = LoadIcon (NULL, IDI_APPLICATION);
    wc.hCursor = LoadCursor (NULL, IDC_ARROW);
    wc.hbrBackground = (HBRUSH) GetStockObject (BLACK_BRUSH);
    wc.lpszMenuName = NULL;
    wc.lpszClassName = "GLSample";
    RegisterClass (&wc);

    /* create main window */
    hWnd = CreateWindow (
      "GLSample", "OpenGL Sample", 
      WS_CAPTION | WS_POPUPWINDOW | WS_VISIBLE,
      0, 0, 256, 256,
      NULL, NULL, hInstance, NULL);

    /* enable OpenGL for the window */
    EnableOpenGL (hWnd, &hDC, &hRC);

    /* program main loop */
    while (!bQuit)
    {
        /* check for messages */
        if (PeekMessage (msg, NULL, 0, 0, PM_REMOVE))
        {
            /* handle or dispatch messages */
            if (msg.message == WM_QUIT)
            {
                bQuit = TRUE;
            }
            else
            {
                TranslateMessage (msg);
                DispatchMessage (msg);
            }
        }
        else
        {
            /* OpenGL animation code goes here */

            glClearColor (0.0f, 0.0f, 0.0f, 0.0f);
            glClear (GL_COLOR_BUFFER_BIT);

            glPushMatrix ();
            glRotatef (theta, 0.0f, 0.0f, 1.0f);
            glBegin (GL_TRIANGLES);
            glColor3f (1.0f, 0.0f, 0.0f);   glVertex2f (0.0f, 1.0f);
            glColor3f (0.0f, 1.0f, 0.0f);   glVertex2f (0.87f, -0.5f);
            glColor3f (0.0f, 0.0f, 1.0f);   glVertex2f (-0.87f, -0.5f);
            glEnd ();
            glPopMatrix ();

            SwapBuffers (hDC);

            theta += 1.0f;
            Sleep (1);
        }
    }

    /* shutdown OpenGL */
    DisableOpenGL (hWnd, hDC, hRC);

    /* destroy the window explicitly */
    DestroyWindow (hWnd);

    return msg.wParam;
}


/********************
 * Window Procedure
 *
 ********************/

LRESULT CALLBACK WndProc (HWND hWnd, UINT message,
                          WPARAM wParam, LPARAM lParam)
{

    switch (message)
    {
    case WM_CREATE:
        return 0;
    case WM_CLOSE:
        PostQuitMessage (0);
        return 0;

    case WM_DESTROY:
        return 0;

    case WM_KEYDOWN:
        switch (wParam)
        {
        case VK_ESCAPE:
            PostQuitMessage(0);
            return 0;
        }
        return 0;

    default:
        return DefWindowProc (hWnd, message, wParam, lParam);
    }
}


/*******************
 * Enable OpenGL
 *
 *******************/

void EnableOpenGL (HWND hWnd, HDC hDC, HGLRC hRC)
{
    PIXELFORMATDESCRIPTOR pfd;
    int iFormat;

    /* get the device context (DC) */
    *hDC = GetDC (hWnd);

    /* set the pixel format for the DC */
    ZeroMemory (&pfd, sizeof (pfd));
    pfd.nSize = sizeof (pfd);
    pfd.nVersion = 1;
    pfd.dwFlags = PFD_DRAW_TO_WINDOW | 
      PFD_SUPPORT_OPENGL | PFD_DOUBLEBUFFER;
    pfd.iPixelType = PFD_TYPE_RGBA;
    pfd.cColorBits = 24;
    pfd.cDepthBits = 16;
    pfd.iLayerType = PFD_MAIN_PLANE;
    iFormat = ChoosePixelFormat (*hDC, &pfd);
    SetPixelFormat (*hDC, iFormat, &pfd);

    /* create and enable the render context (RC) */
    *hRC = wglCreateContext( *hDC );
    wglMakeCurrent( *hDC, *hRC );

}


/******************
 * Disable OpenGL
 *
 ******************/

void DisableOpenGL (HWND hWnd, HDC hDC, HGLRC hRC)
{
    wglMakeCurrent (NULL, NULL);
    wglDeleteContext (hRC);
    ReleaseDC (hWnd, hDC);
}

The error says it can not convert HWND to HWND for one of the functions. What should I do? And what changes should I change to avoid making the mistake again?

It also says "CANNOT CONVERT MSG TO tagMSG for void function."

I have no idea what to do and tried tweaking around and it didn't help. Please some tips.

I just tweaked around with it a little more and now it says the following:

no match for 'operator=' in '*hDC = GetDC[hWnd]'
candidates are: HDC_& HDC_::operator=[const HDC_&]
cannot convert 'HDC_'to 'HDC_' for argument '1' to "int ChoosePixelFormat

And 9 more errors....

Some help would be appreciated.

Please provide the exact error messages you get when compiling the code you have provided.

Please provide the exact error messages you get when compiling the code you have provided.

I just did. They're in my first post all the way on the bottom.

You don't learn pointers by making a big long program like that. Everyone starts at "Hello World" or something similar. None of the tutorials out there that have been linked by various people on this forum do it by taking apart examples like you posted. They're all "Hello World", "Count to ten" type examples. The good ones teach the mechanics of "how", then when you've mastered that, worry about the why. There are a whole bunch of "whys", including extremely fast execution in lots of cases that you can't get with, say, Java.

Here are a couple more I like since you apparently didn't get the tutorial you wanted from others.

One from DaWei:

http://daweidesigns.netfirms.com/cgi-bin/pointers.php

One from Narue:

http://www.eternallyconfuzzled.com/tuts/languages/jsw_tut_pointers.aspx


Narue's (you met her) hits on just above every aspect of them. Read it thoroughly several times, try the examples, THEN come back and post questions on it.

You don't learn pointers by making a big long program like that. Everyone starts at "Hello World" or something similar. None of the tutorials out there that have been linked by various people on this forum do it by taking apart examples like you posted. They're all "Hello World", "Count to ten" type examples. The good ones teach the mechanics of "how", then when you've mastered that, worry about the why. There are a whole bunch of "whys", including extremely fast execution in lots of cases that you can't get with, say, Java.

Here are a couple more I like since you apparently didn't get the tutorial you wanted from others.

One from DaWei:

http://daweidesigns.netfirms.com/cgi-bin/pointers.php

One from Narue:

http://www.eternallyconfuzzled.com/tuts/languages/jsw_tut_pointers.aspx


Narue's (you met her) hits on just above every aspect of them. Read it thoroughly several times, try the examples, THEN come back and post questions on it.

Okay, thanks.

Unfortunately I read both of those links you gave and it still doesn't help me with my program in OpenGL.

And I have done smaller programs, such as just pointing to a random variable and that's it. I can't think of much of any thing else I could use them with, but in that base it's easy. It's simple. Unfortunately, again, it doesn't help with where I'm stuck at.

That's easy. What I need to know is how to use them the harder way, say, with the program I just gave before. If you can help me with that it'd be nice.

>> Unfortunately I read both of those links you gave and it still doesn't help me with my program in OpenGL.


Quick reader. Ten minutes to read two intricate, nuanced tutorials from people with decades of experience between them, plus trying out the examples, one of which spans several pages. You obviously didn't read anything, nor did you read any of the other links anyone else posted. You've been littering the entire forum with comments about not understanding pointers. And to debug this code, you'll need to learn about "handles" too.

>> Unfortunately I read both of those links you gave and it still doesn't help me with my program in OpenGL.


Quick reader. Ten minutes to read two intricate, nuanced tutorials from people with decades of experience between them, plus trying out the examples, one of which spans several pages. You obviously didn't read anything, nor did you read any of the other links anyone else posted. You've been littering the entire forum with comments about not understanding pointers. And to debug this code, you'll need to learn about "handles" too.

Handles like Windows HWND? I know how to use those pretty fairly, what I don't get is pointers 100%. And most codes I use involving pointers never compile. It always says it fails to convert some thing to some thing else of the same value.

And did it ever occur to you or any one that maybe these "advanced" teachers of pointers are not exactly the best at explaining it for every one to elaborately understand the same way?

>> And did it ever occur to you or any one that maybe these "advanced" teachers of pointers are not exactly the best at explaining it for every one to elaborately understand the same way?

Banned from three sites, about to be banned from this one, user name "spoonlicker", 46 down votes in three days, wants to build not only an operating system, but the computer itself from scratch, can't find a SINGLE tutorial on pointers that you think is a good one, etc., and apparently you're having problems with Java too, which has no pointers. There's this beaut here...

I just started to learn Java a few weeks ago but gave up on it after hours of attempts and my code didn't work...

...I saved it as JAVA.java and tried to run it on Eclipse but it says there's an error. The error I forget what it was because it was like 3 weeks or so ago, but it wouldn't bypass the error for nothing.

I eventually grew tired of it and deleted it, but can any one explain why it didn't work?

How's the Operating System coming along? You'll pardon me if I'm a little skeptical.

That said, you're sort of funny at times, the user name cracked me up and I did a Google. You've been around. But some people like mike_2000_17 are actually expending real energy crafting real answers which you obviously couldn't give a damn about and there are real people trying to get real help here and you're hijacking their threads and not to get overly-melodramatic, it does real damage. People feel like crap when they come to a forum for real learning and get suckered into a flame war. There's nothing like writing something up, choosing your words carefully, trying to help, and finding out that the whole thing was a joke, or on the other hand, coming for help, thinking you're getting help, then finding out you weren't. Please consider that when posting. By now you have the red dot, though, so you probably won't get many more serious responses.

Now will my saying that cause you to do it more, less, not have an effect at all, who knows, but what the hell, I'll post it anyway.

[EDIT]
>> By now you have the red dot, though

What happened to the red dot?! You used to be able to tell at half a glance who had been branded, and that was a good thing. Not that you can't find out now pretty easily, but it doesn't have that nice, immediate, impossible to miss warning. I never noticed that was gone before.
[/EDIT]

>> And did it ever occur to you or any one that maybe these "advanced" teachers of pointers are not exactly the best at explaining it for every one to elaborately understand the same way?

Banned from three sites, about to be banned from this one, user name "spoonlicker", 46 down votes in three days, wants to build not only an operating system, but the computer itself from scratch, can't find a SINGLE tutorial on pointers that you think is a good one, etc., and apparently you're having problems with Java too, which has no pointers. There's this beaut here...

How's the Operating System coming along? You'll pardon me if I'm a little skeptical.

That said, you're sort of funny at times, the user name cracked me up and I did a Google. You've been around. But some people like mike_2000_17 are actually expending real energy crafting real answers which you obviously couldn't give a damn about and there are real people trying to get real help here and you're hijacking their threads and not to get overly-melodramatic, it does real damage. People feel like crap when they come to a forum for real learning and get suckered into a flame war. There's nothing like writing something up, choosing your words carefully, trying to help, and finding out that the whole thing was a joke, or on the other hand, coming for help, thinking you're getting help, then finding out you weren't. Please consider that when posting. By now you have the red dot, though, so you probably won't get many more serious responses.

Now will my saying that cause you to do it more, less, not have an effect at all, who knows, but what the hell, I'll post it anyway.

[EDIT]
>> By now you have the red dot, though

What happened to the red dot?! You used to be able to tell at half a glance who had been branded, and that was a good thing. Not that you can't find out now pretty easily, but it doesn't have that nice, immediate, impossible to miss warning. I never noticed that was gone before.
[/EDIT]

Explain what you mean by "I've been around."

If any one is trolling here it's definitely you. I'm just trying to get some small tips and advice on pointers from a completely different perspective than what most web sites and books give, which is not clear enough for me to fully understand, even with testing.

it still doesn't help me with my program in OpenGL.

That's because if you truly don't understand as you say, you have no business delving into Win32 and OpenGL before properly learning the basics of C or C++.

Handles like Windows HWND? I know how to use those pretty fairly, what I don't get is pointers 100%.

What the hell do you think a handle is? It's a pointer!

And did it ever occur to you or any one that maybe these "advanced" teachers of pointers are not exactly the best at explaining it for every one to elaborately understand the same way?

Correct. Different people respond better to different explanations of the same concept. But for those as violently opposed to learning as you are, any explanation at all is wasted effort. If you have a specific issue with any explanation of pointers, feel free to be specific about the specific issue and I'll try to word it in a way that you might understand. I'll do this not because you might finally grasp the concept, but that others who are actually trying to learn will be helped by it.

Such is the benefit of a public forum. If this were private correspondence, I would have stopped responding to you long ago.

apparently you're having problems with Java too, which has no pointers

Java has a restricted form of pointers in the reference. It's actually rather important to recognize this when learning Java.

That's because if you truly don't understand as you say, you have no business delving into Win32 and OpenGL before properly learning the basics of C or C++.


What the hell do you think a handle is? It's a pointer!


Correct. Different people respond better to different explanations of the same concept. But for those as violently opposed to learning as you are, any explanation at all is wasted effort. If you have a specific issue with any explanation of pointers, feel free to be specific about the specific issue and I'll try to word it in a way that you might understand. I'll do this not because you might finally grasp the concept, but that others who are actually trying to learn will be helped by it.

Such is the benefit of a public forum. If this were private correspondence, I would have stopped responding to you long ago.


Java has a restricted form of pointers in the reference. It's actually rather important to recognize this when learning Java.

Maybe I should invent a programming language that uses no pointers....Because if HWND is a pointer and a pointer is & and * and HWND is considered a Windows lvalue assignment then I'm totally done here. This <snip> is too goddamn confusing. And that hostility is showing you how much of a struggle I'm in.

If you can't understand pointers, you'll have a hard time inventing a programming language. But if you can do it, more power to you.

You know there is this minimalist programming language called brainf*ck which is a Turing Complete language (i.e. you can program anytime you like with it) and it has 8 operators and only 1 variable... a pointer! So, even the smallest programming language in the world has pointers!

Are you really trying to understand pointers or are you just trying to get someone to explain the copy-and-paste code you found somewhere? As someone in this thread already stated, I'd recommend working on simple programs to get more of an understanding as to how pointers work.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.