C++ incremental pointer.

Started by
7 comments, last by Storyyeller 12 years, 10 months ago
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);
add(attack);

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.
Advertisement
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.
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.
Moving to For Beginners.

Wielder of the Sacred Wands
[Work - ArenaNet] [Epoch Language] [Scribblings]

what abou trying y++; ?

Never say Never, Because Never comes too soon. - ryan20fun

Disclaimer: Each post of mine is intended as an attempt of helping and/or bringing some meaningfull insight to the topic at hand. Due to my nature, my good intentions will not always be plainly visible. I apologise in advance and assure you I mean no harm and do not intend to insult anyone.


what abou trying y++; ?


Y += 1 is the exact same as y++ mate.
y++ would still be pointer arithmetic, which doesn't seem to be what the OP intends.
just thought it would trigger the IDE to use the correct type

Never say Never, Because Never comes too soon. - ryan20fun

Disclaimer: Each post of mine is intended as an attempt of helping and/or bringing some meaningfull insight to the topic at hand. Due to my nature, my good intentions will not always be plainly visible. I apologise in advance and assure you I mean no harm and do not intend to insult anyone.

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.
I trust exceptions about as far as I can throw them.

This topic is closed to new replies.

Advertisement