Pointer deferenece of typical fx crashing program

int * width = 0;
int * height = 0;

glfwGetWindowSize(window, width, height);

so far no problems!
add:

cout<<*width

program crashes

You need to pass address of integer where glfwGetWindowSize can write to. Currently you are passing NULL pointers, so it doesn’t write anything. and when you dereference width, the pointer is still NULL, thus the crash.

Here’s how to pass address to variables allocated on stack:

int width, height;
glfwGetWindowSize(window, &width, &height);
1 Like

Thx works fine!

My confusion still lingers. We just passed by reference when the definition passes pointer-types:

void glfwGetWindowSize (GLFWwindow *window, int *width, int *height)

What am I missing?

We are not passing by reference. GLFW is C library, it doesn’t have references.
If you look at function declaration you can see it accepts addresses to int’s for width and height. So you need to pass address. Not reference. For references the declaration of function would this:

void glfwGetWindowSize (GLFWwindow *window, int& width, int& height)

You get address of variable with & operator:

int width;
int* address_of_width = &width;

glfwGetWindowSize(window, address_of_width, address_of_height);
glfwGetWindowSize(window, &width, &height); // same thing as line above

Alternatively you can allocate on heap and free it later:

int* width = new int; // or malloc
glfwGetWindowSize(window, width, height);
... // use width here
delete width; // free the memory
1 Like

function def contains pointers:
pointer func def

But the type def has pointers not ints:

void glfwGetWindowSize (GLFWwindow *window, **int width, int height)

Thank you in advance. The help so far has been helpful.

Exactly! That was just an example how would function look like if you could pass references. But it does not look like that. So you need to pass pointers, not references.

But your code works:

int width, height;
glfwGetWindowSize(window, &width, &height);

without passing pointers *width, *height
I am so confused

IThat was my question at the beginning of the thread - how to pass pointers for function

glfwGetWindowSize(window, *width, *height);

You are misunderstanding what is pointer and what is reference.

int x;

This is int variable on stack.

&x

this is address of x. It can be assigned to pointer to int:

int *y = &x;

or it can be passed to function that accepts pointer to int void f(int* arg):

f(&x);
f(y); // same thing

If width has int type, you cannot dereference it with *width syntax. That’s invalid. You either use value by writing width, or write &width to get address of width variable which is int pointer (int* type).

1 Like

By adding ‘&’ to integer (e.g. width) its type becomes pointer-type.
Now that the type matches the parameter type of the function, it will work correctly.