Search code examples
cwindowsdl-2

c - error: invalid operands to binary * (have 'int' and 'SDL_Window *') when attempting to use SDL_CreateWindow


As seen on this issue here I had a few errors while trying to create a window, which are now fixed with this code:

#include <stdio.h>
#define SDL_MAIN_HANDLED
#include <SDL2/SDL.h>
#include <SDL2/SDL_events.h>
#include <SDL2/SDL_video.h>

int main() {
    int SDL_Window;
    SDL_Window * SDL_CreateWindow("title", SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED, 720, 480, 0);
}

However, there's a new error now:

main.c: In function 'main':
main.c:9:20: error: invalid operands to binary * (have 'int' and 'SDL_Window *')
    9 |         SDL_Window * SDL_CreateWindow("title", SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED, 720, 480, 0);
      |                    ^ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
      |                      |
      |                      SDL_Window *

replacing the last line by SDL_Window = SDL_CreateWindow("title", SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED, 720, 480, 0); doesen't trigger an error anymore, but it does give me a warning:

main.c: In function 'main':
main.c:9:20: warning: assignment to 'int' from 'SDL_Window *' makes integer from pointer without a cast [-Wint-conversion]
    9 |         SDL_Window = SDL_CreateWindow("title", SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED, 720, 480, 0);
      |                    ^

Nontheless,when running the executable a window does appear, but then instantly crash, i'm not sure if this is intended or not since there's nothing after interacting with the window, but a warning is probably not a good sign. I figured it might have something to do with pointers, but I am way too much of a beginner to understand what causes this.


Solution

  • The issue appears to be that your code declares the variable SDL_Window to be an int, when you are assigning it to the result of a function call that returns an SDL_Window * (link to documentation for SDL_CreateWindow()). To fix this issue, try changing this line:

    int SDL_Window;
    

    to

    SDL_Window * SDL_Window
    

    Also I would recommend changing the name of the variable SDL_Window to something else, such as window. This is to avoid confusion between the type named SDL_Window and the variable named SDL_Window.

    To prevent the window from instantly "crashing", try calling scanf() after calling SDL_CreateWindow(). After the window opens, enter a key in the terminal you ran your program from and then press the "enter" key to close it. Also, don't forget to call SDL_Quit() to quit SDL at the end of your program to free your process's resources and return 0 to signify that your program ran successfully.

    Full example

    #include <stdio.h>
    #define SDL_MAIN_HANDLED
    #include <SDL2/SDL.h>
    #include <SDL2/SDL_events.h>
    #include <SDL2/SDL_video.h>
    
    int main() {
            SDL_Window *window; // Notice how I changed variable type and name.
            window = SDL_CreateWindow("title", SDL_WINDOWPOS_UNDEFINED,
                                      SDL_WINDOWPOS_UNDEFINED, 720, 480, 0);
            scanf(" ");
            SDL_Quit();
            return 0;
    }
    

    You may find this tutorial series helpful for learning more about SDL2.