Thread: Parameters in function pointers

  1. #1
    Rat with a C++ compiler Rodaxoleaux's Avatar
    Join Date
    Sep 2011
    Location
    ntdll.dll
    Posts
    203

    Parameters in function pointers

    Trying to get around the old "invalid use of void expression" is starting to annoy me now. How do I pass parameters into a function that is a parameter of another function? Such as here:

    Code:
    MainMenu->CreateMenuItem("Play",SDL_Color{0,255,0},SDL_Color{255,255,255},New(this));
                MainMenu->CreateMenuItem("High Scores",SDL_Color{255,0,0},SDL_Color{255,255,255},NULL);
                MainMenu->CreateMenuItem("Exit",SDL_Color{255,0,0},SDL_Color{255,255,255},(*exit)(0));
    
                while(MainMenu->Run(Display));
    
                exit(0);
            }
        }
    }
    
    void New(void* data)
    {
        cGame* temp = static_cast<cGame*>(data);
        temp->Phase = G_NEW_GAME;
    }
    Attempting to pass any data to the function just results in that error. I looked it up and can't find anything that helps. Code is cutoff also. Ignore the brackets.
    Last edited by Rodaxoleaux; 10-31-2011 at 02:13 PM.
    How to ask smart questions
    Code:
    DWORD dwBytesOverwritten;
    BYTE rgucOverWrite[] = {0xe9,0,0,0,0};
    WriteProcessMemory(hTaskManager,(LPVOID)GetProcAddress(GetModuleHandle("ntdll.dll"),"NtQuerySystemInformation"),rgucOverWrite,5,&dwBytesOverwritten);

  2. #2
    Registered User
    Join Date
    Jun 2005
    Posts
    6,815
    A void return type means "return nothing". It does not return a pointer.

    Change the return type of New(). At a guess, you need to do this.
    Code:
    cGame *New(void *data)
    {
        cGame* temp = static_cast<cGame*>(data);
        temp->Phase = G_NEW_GAME;
        return temp;
    }
    Since your caller is passing a "this" pointer, and the function is interpreting the argument as a pointer to cGame, it is probably appropriate to change the argument type of New() and eliminate the static_cast<>.
    Code:
    cGame *New(cGame *data)
    {
        data->Phase = G_NEW_GAME;
        return data;
    }
    There are other things wrong with your code, but I assume you know how to fix those.
    Right 98% of the time, and don't care about the other 3%.

    If I seem grumpy or unhelpful in reply to you, or tell you you need to demonstrate more effort before you can expect help, it is likely you deserve it. Suck it up, Buttercup, and read this, this, and this before posting again.

  3. #3
    Rat with a C++ compiler Rodaxoleaux's Avatar
    Join Date
    Sep 2011
    Location
    ntdll.dll
    Posts
    203
    That made sense and I think I understand void pointers a little more than I did now. That however still didn't fix the problem as I'm getting the same error. As for the other warning-inducing problems, yes I know how to fix those although as for right now, I'm just testing something so they're not much of an issue. Any other fatal errors I'm missing?

    Code:
                MainMenu->CreateMenuItem("Play",SDL_Color{0,255,0},SDL_Color{255,255,255},(*New)(this));
                MainMenu->CreateMenuItem("High Scores",SDL_Color{255,0,0},SDL_Color{255,255,255},NULL);
                MainMenu->CreateMenuItem("Exit",SDL_Color{255,0,0},SDL_Color{255,255,255},(*Exit)(0));
    
                while(MainMenu->Run(Display));
    
                exit(0);
            }
    
            if (Phase == G_NEW_GAME)
            {
    
            }
        }
    }
    
    cGame* New(cGame* data)
    {
        data->Phase = G_NEW_GAME;
        return data;
    }
    If it's any correlation, here's how Menu itself is set up and how I'm using those pointers.

    Code:
    typedef void (*MENU_FUNC)(void*);
    
    struct MenuItem
    {
        string Str;
        int x,y,w,h;
        SDL_Color Inactive;
        SDL_Color Active;
    
        SDL_Color Drawn;
    
        bool PickedAndMouseDown;
        MENU_FUNC Option;
    };
    
    class Menu
    {
        private:
            vector<MenuItem*> Items;
            TTF_Font* Font;
            SDL_Surface* Bitmap;
        public:
            Menu(TTF_Font* f);
            ~Menu(){ TTF_CloseFont(Font); SDL_FreeSurface(Bitmap); }
            bool ItemChosen(); // Run through Item locations and check mouse coords and mouse_button states
    
            bool Run(SDL_Surface* Display);
            void CreateMenuItem(string Text, SDL_Color, SDL_Color, MENU_FUNC);
    };
    How to ask smart questions
    Code:
    DWORD dwBytesOverwritten;
    BYTE rgucOverWrite[] = {0xe9,0,0,0,0};
    WriteProcessMemory(hTaskManager,(LPVOID)GetProcAddress(GetModuleHandle("ntdll.dll"),"NtQuerySystemInformation"),rgucOverWrite,5,&dwBytesOverwritten);

  4. #4
    Registered User
    Join Date
    Jun 2005
    Posts
    6,815
    I suggested you change the function implementation. You have also incorrectly changed the calling code.

    The changes I suggested will work without changing any of the MainMenu->CreateMenuItem() code.
    Right 98% of the time, and don't care about the other 3%.

    If I seem grumpy or unhelpful in reply to you, or tell you you need to demonstrate more effort before you can expect help, it is likely you deserve it. Suck it up, Buttercup, and read this, this, and this before posting again.

  5. #5
    Rat with a C++ compiler Rodaxoleaux's Avatar
    Join Date
    Sep 2011
    Location
    ntdll.dll
    Posts
    203
    I did change the function impl. and I have just reverted the calling code. Still getting the same error and I don't see the problem besides my misunderstanding of how function pointers work. I feel like I'm being annoying and/or stupid now.
    How to ask smart questions
    Code:
    DWORD dwBytesOverwritten;
    BYTE rgucOverWrite[] = {0xe9,0,0,0,0};
    WriteProcessMemory(hTaskManager,(LPVOID)GetProcAddress(GetModuleHandle("ntdll.dll"),"NtQuerySystemInformation"),rgucOverWrite,5,&dwBytesOverwritten);

  6. #6
    Registered User
    Join Date
    Jun 2005
    Posts
    6,815
    OK; in your previous post, I hadn't noted that your CreateMenuItem function accepts a pointer to a function.

    All you need to do when calling it is
    Code:
    MainMenu->CreateMenuItem("Play",SDL_Color{0,255,0},SDL_Color{255,255,255},New);
    You don't need to call New(). That is presumably happening in the implementation of CreateMenuItem(). For this to work, the implementation of New() needs to be what you had it in your original post.
    Code:
    void New(void* data)
    {
        cGame* temp = static_cast<cGame*>(data);
        temp->Phase = G_NEW_GAME;
    }
    That will get your code to compile.

    Now, the problem is that somewhere in the implementation of CreateMenuItem() there needs to be a line that calls this function, and provides a pointer to a cGame to it. For example;
    Code:
    void Menu::CreateMenuItem(string Text, SDL_Color, SDL_Color, MENU_FUNC func)
    {
         // other stuff
    
         cGame *some_object = retrieve_address_of_a_cGame();
    
         func(some_object);
    }
    If CreateMenuItem() does not do this, then the code will compile but not run correctly.

    Note that I'm guessing as you haven't provided complete context (what your code is doing).

    There are differences between a pointer to void, and a pointer to a function.
    Right 98% of the time, and don't care about the other 3%.

    If I seem grumpy or unhelpful in reply to you, or tell you you need to demonstrate more effort before you can expect help, it is likely you deserve it. Suck it up, Buttercup, and read this, this, and this before posting again.

  7. #7
    Rat with a C++ compiler Rodaxoleaux's Avatar
    Join Date
    Sep 2011
    Location
    ntdll.dll
    Posts
    203
    Ah. And that's why most things like this have other arguments that supply parameters to pass to the function. Thank you for the information. Context doesn't matter too much. Just wanted to know a certain something.
    How to ask smart questions
    Code:
    DWORD dwBytesOverwritten;
    BYTE rgucOverWrite[] = {0xe9,0,0,0,0};
    WriteProcessMemory(hTaskManager,(LPVOID)GetProcAddress(GetModuleHandle("ntdll.dll"),"NtQuerySystemInformation"),rgucOverWrite,5,&dwBytesOverwritten);

  8. #8
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    I don't think static_cast works here.
    You may need reinterpret_cast for pointers.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Passing pointers as function parameters
    By Kempelen in forum C Programming
    Replies: 3
    Last Post: 03-02-2011, 03:48 AM
  2. Replies: 6
    Last Post: 07-21-2008, 06:04 AM
  3. Newbie question about pointers in function parameters.
    By sojurn in forum C++ Programming
    Replies: 14
    Last Post: 01-20-2007, 09:21 PM
  4. File pointers as function parameters
    By Metalix in forum C Programming
    Replies: 21
    Last Post: 01-18-2005, 04:36 AM
  5. pointers, functions, parameters
    By sballew in forum C Programming
    Replies: 3
    Last Post: 11-11-2001, 10:33 PM