ciaran wickham ciaran wickham - 1 year ago 68
C++ Question

How to use int pointer?

I have two integer pointers that hold the data for the (SDL) screens height and width in pixels. They're declared here:

int *w = nullptr;
int *h = nullptr;


Given values here:

int SDL_GetRendererOutputSize(SDL_Renderer* renderTarget, int *w, int *h);


And used here:

Dest.w = (*w / 2.0f - 58 / 2.0f, *h / 2.0f - 8 / 2.0f);
Dest.h = (*w / 2.0f - 58 / 2.0f, *h / 2.0f - 8 / 2.0f);
Dest.x = (*w / 2.0f - 58 / 2.0f, *h / 2.0f - 8 / 2.0f);
Dest.y = (*w / 2.0f - 58 / 2.0f, *h / 2.0f - 8 / 2.0f);


However *w and *h stay nullptrs. Why Is This?

Answer Source

It's common practice in C (and to a certain extent C++) that if you are writing a function that returns multiple values, the caller has to supply a pointer to the variables which the outputs will be placed into.

So, you would call this function like this:

int w=0,h=0;

int result = SDL_GetRendererOutputSize(renderTarget, &w, &h);
if (result!=0) {
    // handle an error here
}

Note that w and h are normal integer variables. I am passing the address of these variables to SDL_GetRendererOutputSize (the & operator takes the address of it's argument); the function presumably sets the variables.

Don't forget to consult the return value of the function, which in this case is non zero if there was an error.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download