Jump to content

  • Log In with Google      Sign In   
  • Create Account


C++ incremental pointer.


Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

  • You cannot reply to this topic
8 replies to this topic

#1 DriveByBaptism   Members   -  Reputation: 122

Like
0Likes
Like

Posted 09 June 2011 - 11:16 AM

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.

Sponsor:

#2 rip-off   Moderators   -  Reputation: 8165

Like
0Likes
Like

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.

#3 frob   Moderators   -  Reputation: 20513

Like
2Likes
Like

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.
  • 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.

Check out my personal indie blog at bryanwagstaff.com.

#4 ApochPiQ   Moderators   -  Reputation: 15170

Like
0Likes
Like

Posted 09 June 2011 - 06:33 PM

Moving to For Beginners.

#5 ryan20fun   Members   -  Reputation: 986

Like
-1Likes
Like

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.

#6 DriveByBaptism   Members   -  Reputation: 122

Like
0Likes
Like

Posted 11 June 2011 - 04:44 AM

what abou trying

y++;
?


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

#7 rip-off   Moderators   -  Reputation: 8165

Like
0Likes
Like

Posted 11 June 2011 - 05:04 AM

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

#8 ryan20fun   Members   -  Reputation: 986

Like
-2Likes
Like

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.

#9 Storyyeller   Members   -  Reputation: 212

Like
0Likes
Like

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.




Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.



PARTNERS