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.
C++ incremental pointer.
Started by DriveByBaptism, Jun 09 2011 11:16 AM
8 replies to this topic
Ad:
#2 Moderators - Reputation: 5027
Posted 09 June 2011 - 11:37 AM
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.
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.
#3 Moderators - Reputation: 7557
Posted 09 June 2011 - 12:27 PM
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.
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.
#4 Moderators - Reputation: 7472
Posted 09 June 2011 - 06:33 PM
Moving to For Beginners.
Maker of Machinery
[Work - ArenaNet] [Epoch Language] [Scribblings] [Journal - peek into my shattered mind]
[Work - ArenaNet] [Epoch Language] [Scribblings] [Journal - peek into my shattered mind]
#5 Members - Reputation: 333
Posted 11 June 2011 - 12:07 AM
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.
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.
#8 Members - Reputation: 333
Posted 11 June 2011 - 05:49 AM
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.
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.
#9 Members - Reputation: 212
Posted 11 June 2011 - 10:02 AM
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.






