943,800 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Marked Solved
  • Views: 756
  • C++ RSS
You are currently viewing page 1 of this multi-page discussion thread
Aug 19th, 2009
0

Int assigning problem

Expand Post »
I have this piece of code
C++ Syntax (Toggle Plain Text)
  1. // some function
  2. int px;
  3. void SomeFunction( void *dummy )
  4. {
  5. ...
  6.  
  7. RECT rect;
  8. GetWindowRect(hWnd, &rect);
  9. px = ((rect.right - rect.left) / 2) - 60;
  10.  
  11. ...
  12.  
  13. // Terminate thread
  14. _endthread();
  15. }

And my problem is px doesn't get assigned.. it stays 0.. although in the debugger I can see all the values inside rect..
please help me
Similar Threads
Reputation Points: 10
Solved Threads: 0
Newbie Poster
mcco1 is offline Offline
10 posts
since Jul 2009
Aug 19th, 2009
0

Re: Int assigning problem

What are the values of RECT members?
Reputation Points: 113
Solved Threads: 20
Junior Poster
Laiq Ahmed is offline Offline
147 posts
since Jun 2006
Aug 19th, 2009
0

Re: Int assigning problem

Click to Expand / Collapse  Quote originally posted by Laiq Ahmed ...
What are the values of RECT members?
http://msdn.microsoft.com/en-us/libr...97(VS.85).aspx
c++ Syntax (Toggle Plain Text)
  1. typedef struct _RECT {
  2. LONG left;
  3. LONG top;
  4. LONG right;
  5. LONG bottom;
  6. }RECT, *PRECT;

The values are numbers
Reputation Points: 10
Solved Threads: 0
Newbie Poster
mcco1 is offline Offline
10 posts
since Jul 2009
Aug 19th, 2009
0

Re: Int assigning problem

Thank you mcco for providing me the definition of RECT structure

kindly provide me the values (numbers), It seems that your expression evaluates to zero thats why I've asked to provide me the values not the RECT members. .
Reputation Points: 113
Solved Threads: 20
Junior Poster
Laiq Ahmed is offline Offline
147 posts
since Jun 2006
Aug 19th, 2009
1

Re: Int assigning problem

Click to Expand / Collapse  Quote originally posted by mcco1 ...
I have this piece of code
C++ Syntax (Toggle Plain Text)
  1. // some function
  2. int px;
  3. void SomeFunction( void *dummy )
  4. {
  5. ...
  6.  
  7. RECT rect;
  8. GetWindowRect(hWnd, &rect);
  9. px = ((rect.right - rect.left) / 2) - 60;
  10.  
  11. ...
  12.  
  13. // Terminate thread
  14. _endthread();
  15. }

And my problem is px doesn't get assigned.. it stays 0.. although in the debugger I can see all the values inside rect..
please help me
What about if you change px from int to long? Does that give you any differemt results?
The thing to take note of here is that px is currently declared as an int. Whereas rect.right and rect.left are both LONG/long variables.

Therefore if the result of your calculation is less than 1.0 (e.g. something like 0.43455664), then because px is an int, the calculated value will be truncated to 0.
Whereas if you alter px to be long, you'll get the actual calculated value (in my example 0.4345564).

I could be wrong, but that looks like the most obvious explanation to me!

oh, hang on.....Looking at that code, another thing strikes me as odd....
px is declared outside of the function, but used inside it..
That's probably your problem right there!
The px used in the function and the px outside of the function might not be the same variable, they might have different scope.
If you could post some more code, we might be able to determine whether or not this is the case!

Cheers for now,
Jas.
Last edited by JasonHippy; Aug 19th, 2009 at 11:33 am.
Reputation Points: 590
Solved Threads: 123
Practically a Master Poster
JasonHippy is offline Offline
672 posts
since Jan 2009
Aug 19th, 2009
1

Re: Int assigning problem

Click to Expand / Collapse  Quote originally posted by JasonHippy ...
What about if you change px from int to long? Does that give you any differemt results?
The thing to take note of here is that px is currently declared as an int. Whereas rect.right and rect.left are both LONG/long variables.

Therefore if the result of your calculation is less than 1.0 (e.g. something like 0.43455664), then because px is an int, the calculated value will be truncated to 0.
Whereas if you alter px to be long, you'll get the actual calculated value (in my example 0.4345564).
A 'long' variable in this context is still an integer, not a float, so changing px to long will make no difference.
The most likely explanation, as you suggested, is that px has also been declared locally and the OP is examining the global variable.
Reputation Points: 76
Solved Threads: 40
Junior Poster
MrSpigot is offline Offline
156 posts
since Mar 2009
Aug 19th, 2009
0

Re: Int assigning problem

Try
::px = ((rect.right - rect.left) / 2) - 60;

I am assuming there is a local variable hiding the global.
Reputation Points: 840
Solved Threads: 594
Senior Poster
firstPerson is offline Offline
3,862 posts
since Dec 2008
Aug 19th, 2009
0

Re: Int assigning problem

Check out if you have declared a variable inside SomeFunction with the same name as px, in this case the local variable is used and the global variable is cached.
Reputation Points: 10
Solved Threads: 1
Newbie Poster
Benamar is offline Offline
2 posts
since Aug 2009
Aug 19th, 2009
0

Re: Int assigning problem

Sorry the global variable is hidden not cached
Reputation Points: 10
Solved Threads: 1
Newbie Poster
Benamar is offline Offline
2 posts
since Aug 2009
Aug 19th, 2009
0

Re: Int assigning problem

Click to Expand / Collapse  Quote originally posted by JasonHippy ...
oh, hang on.....Looking at that code, another thing strikes me as odd....
px is declared outside of the function, but used inside it..
That's probably your problem right there!
The px used in the function and the px outside of the function might not be the same variable, they might have different scope.
If you could post some more code, we might be able to determine whether or not this is the case!

Cheers for now,
Jas.
Thanks! that was my problem!
this function is pretty long and I forgot that I have another px in it.
Reputation Points: 10
Solved Threads: 0
Newbie Poster
mcco1 is offline Offline
10 posts
since Jul 2009

This thread is solved

Either the thread starter or a moderator has marked this thread as solved. You can most likely trust the responses and answers given. There is most likely no reason for any further responses to be posted here. If you have a related question, please start a new thread in this forum instead.

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C++ Forum Timeline: Help Me Out Hamming Code
Next Thread in C++ Forum Timeline: Printing a diamond





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC