Sign in to follow this  
DriveByBaptism

C++ incremental pointer.

Recommended Posts

DriveByBaptism    122
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.

Share this post


Link to post
Share on other sites
rip-off    10979
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 this post


Link to post
Share on other sites
frob    44973
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.
[list][*]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 [url="http://www.parashift.com/c++-faq-lite/misc-technical-issues.html#faq-39.1"]formatted streaming into a stringstream[/url] so you can validate at compile time that your conversion is legal.[/list]

Share this post


Link to post
Share on other sites
Storyyeller    215
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.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this