I wonder when to pass a pointer and when to pass a variable address to a function

Hello,

I’m trying to grasp when to pass a pointer and when to pass a variable address to a function. For example, the SDL_PollEvent function takes an event pointer as an argument, but the example code passes the address of a variable of type SDL_Event.

The documentations look like this:
int SDL_PollEvent(SDL_Event* event)

`while (1) {
    SDL_Event event;
    while (SDL_PollEvent(&event)) { 
        /* handle your event here */
    }
    /* do some other stuff here -- draw your app, etc. */
}`

This confuses me, I tried to experiment and pass a pointer instead but either I’m doing something wrong or the code breaks when a pointer is passed. Down below I post the link to the SDL_PollEvent, I would really appreciate it if anyone could explain this to me.

http://wiki.libsdl.org/SDL_PollEvent?highlight=(\bCategoryAPI\b)|(SDLFunctionTemplate)

I was testing this out a bit more, and have come up with a theory. It might be wrong … :sweat_smile:

If I write the code down below, It works to pass a pointer, my problem was that I passed a nullptr because I thought the pointer would be initialized and then mutated by the SDL_PollEvent. But the pointer is not pointing at anything that can be initialized or mutated.

If I pass an address of a variable of the type SDL_Event, then I pass a bit of memory not initialized but is set up to be mutated by the SDL_PollEvent, the first mutation will be an initialization and then it will keep mutating the variable at the memory location.

Because I only use the pointer to point to the memory address and then I don’t do anything more with it it can be skipped and the address of the SDL_event variable can be passed. Less code is more bug proof so that is the way to go :smiley:

 SDL_Event e;
 SDL_Event* eventPtr = &e;

  while (isRunning) {
    while (SDL_PollEvent(eventPtr) != 0 && isRunning) {
      if (eventPtr->type == SDL_QUIT) {
        isRunning = 0;
      }
    }
  }