1

I'm learning GLFW 3.3, and, as it's said in functions description:

Returns the previously set callback, or NULL if no callback was set.

Source: [https://www.glfw.org/docs/3.3/group__init.html#gaff45816610d53f0b83656092a4034f40]

Now what I'm trying to do is to understand what kind of value it is. I tried to understand it as if the return value was a pointer to a function, because, as said about GLFWerrorfun type in the documentation:

typedef void(* GLFWerrorfun) (int, const char *) This is the function pointer type for error callbacks.

Source: [https://www.glfw.org/docs/3.3/group__init.html#ga6b8a2639706d5c409fc1287e8f55e928]

Here's my code:

#define GLFW_DLL
#include <glad/glad.h>
#include <GLFW/glfw3.h>
#include <iostream>

void handleErrorCallback (int code, const char * description)
{
    // Code
}

int main (int argc, char * argv[])
{
    std::cout << glfwSetErrorCallback (handleErrorCallback) << std::endl;
    std::cout << glfwSetErrorCallback (NULL) << std::endl;

    return 0;
}

The first value written on the console is 0. I get it as the NULL was returned because no callback was set yet.

The second value written on the console is 1, and it confuses me: how did GLFWerrorfun return value was converted to an integer? All I see is that it's always returns '1' when the callback function was set and always '0' when it wasn't.

CoSalamander
  • 121
  • 7
  • 1
    Because `std::cout` interprets pointers as integers. Related: https://stackoverflow.com/questions/2064692/how-to-print-function-pointers-with-cout – pptaszni Oct 28 '21 at 14:50
  • 1
    I think this should help: https://stackoverflow.com/questions/2064692/how-to-print-function-pointers-with-cout –  Oct 28 '21 at 15:05
  • read the [docs](https://www.glfw.org/docs/3.0/group__error.html#gaa5d796c3cf7c1a7f02f845486333fb5f) to find out what it returns – bb1950328 Oct 28 '21 at 15:12
  • @bb1950328 this document is commented in my question. Sorry, but you don't point at the answer. – CoSalamander Oct 28 '21 at 16:04
  • @CoSalamander it's not the same document. But the answer is also in the document you linked. Read carefully what `glfwSetErrorCallback` returns. (It's "The previously set callback, or NULL if no callback was set or an error occurred.") – bb1950328 Oct 28 '21 at 18:52
  • @bb1950328 Ok, I modified the header so it'll be more obvious that semantics of the questions description is connected to that header. – CoSalamander Oct 29 '21 at 14:24

1 Answers1

1

glfwSetErrorCallback returns a pointer wich is interpreted as an integer.

I think this post will help:How to print function pointers with cout?

  • So, it should be enough to modify the output as: `std::cout << ((void*) glfwSetErrorCallback (handleErrorCallback)) << std::endl; std::cout << ((void*) glfwSetErrorCallback (NULL)) << std::endl;` – CoSalamander Oct 28 '21 at 16:16