C++ incremental pointer.

This topic is 2417 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

Recommended Posts

I'm using OpenGL with GLFW and I'm trying to get a number to display, and increment when I click a button.

int increment = 1;
y = 1;
char labelString[1];
sprintf(labelString, "%u", y);
attack = new GuiLabel(FontManager::font("medium"), labelString, -280, -70, 1.0, true);

Some notes:

y is a pointer to an integer stored in the header file.
GuiLabel, is what I'm using to display text. It sets the font, what you want to say, x pos, y pos, scale and shadow.

In my button code, I've simply got it going

y += 1;

but I'm getting the following error with this, that I don't know how to defeat:

cannot convert from 'int' to 'int *

Tried googling, but got nothing.

Share on other sites
You probably want to dereference the pointer. E.g. *y = 1, *y += 1. Just make sure the pointer points somewhere valid first.

You shouldn't be defining variables in header files, doing so will lead to multiple definition errors eventually. You can probably refactor your ccde to remove the need for such a global.

Share on other sites
There are several issues in your code.

y = 1;
What you wrote is telling it to point to memory address 1. You need to say (*y) = 1; for the behavior you said you wanted. That assumes you have already pointed y to something meaningful. Dereferencing an invalid pointer is undefined behavior (a bug).

char labelString[1];
This won't hold a string. At best it can hold an empty string, since the string terminator is one character long. You should use a class like std::string or std::stringstream to manage your memory automatically.

sprintf(labelString, "%u", y);
Several errors here.
• labelString is too short. You will have a buffer overrun (a bug).
• y is a pointer not a number. You need to dereference it with (*y) to find the number being pointed to.
• signed/unsigned mismatch. For int you can only use %d or %i, for unsigned int you can only use %o, %u, %x, %X. Only a few compilers actually check for this, but using the wrong value is undefined behavior (a bug). That is one reason to prefer the next one...
• use the std::string class, or formatted streaming into a stringstream so you can validate at compile time that your conversion is legal.

Share on other sites
Moving to For Beginners.

Share on other sites
what abou trying y++; ?

Share on other sites

what abou trying y++; ?

Y += 1 is the exact same as y++ mate.

Share on other sites
y++ would still be pointer arithmetic, which doesn't seem to be what the OP intends.

Share on other sites
just thought it would trigger the IDE to use the correct type

Share on other sites
That's not how C++ works. Unless you specifically dereference the pointer using * or ->, anything you do with it will be done to the pointer, not the target. The IDE isn't going to magically read your mind and decide to do something other than what you told it to do.