Is there reason to create window first and then initialize the library?

yj1214 wrote on Sunday, November 01, 2015:

Is there difference between this code,

int main(void)
{
    GLFWwindow* window;

    glfwInit()

and this code?

int main(void)
{
    glfwInit();
    
    GLFWwindow* window;

There is no functional difference.

The code GLFWwindow* window; doesn’t create a window - it creates (technically it declares and defines) a pointer of type GLFWwindow. Doing so doesn’t call any GLFW functions, and so can happen prior to glfwInit().

yj1214 wrote on Sunday, November 01, 2015:

Few questions…

  1. What is GLFWwindow type? is it an int?

  2. It wouldn’t matter even if I create window first and then initlaize, or do the opposite, right?

int main(void)
{
    GLFWwindow* window = glfwCreateWindow(.....);

    glfwInit()
}

and

int main(void)
{
    glfwInit();
    
    GLFWwindow* window = glfwCreateWindow(.....);
}

bastiaanolij wrote on Friday, January 01, 2016:

Hi yj1214,

That last example does matter, while defining the pointer as in your first post can be done at any time prior to calling glfwCreateWindow, glfwInit must be called before glfwCreateWindow is called. glfwInit does all the preparations before GLFW can do its stuff.

GLFWwindow is a structure containing various bits of information about the window. Variable “window” contains a pointer to that structure. Variable “window” is undefined when you declare it and depending on the compiler is either set to a NULL pointer or has whatever bogus value was in memory at that location before the application was loaded. glfwCreateWindow creates the actual window, stores the information about that window somewhere in memory using the structure GLFWwindow and returns a pointer to that structure that you then store in the variable “window” for future use.

Here is some more info on pointers:
http://www.cprogramming.com/tutorial/c/lesson6.html

The data type of a pointer depends on the platform but is likely a 32bit int.