Int assigning problem

Please support our C++ advertiser: Intel Parallel Studio Home
Thread Solved

Join Date: Jul 2009
Posts: 8
Reputation: mcco1 is an unknown quantity at this point 
Solved Threads: 0
mcco1 mcco1 is offline Offline
Newbie Poster

Int assigning problem

 
0
  #1
Aug 19th, 2009
I have this piece of code
  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
Reply With Quote Quick reply to this message  
Join Date: Jun 2006
Posts: 147
Reputation: Laiq Ahmed will become famous soon enough Laiq Ahmed will become famous soon enough 
Solved Threads: 20
Laiq Ahmed Laiq Ahmed is offline Offline
Junior Poster

Re: Int assigning problem

 
0
  #2
Aug 19th, 2009
What are the values of RECT members?
Reply With Quote Quick reply to this message  
Join Date: Jul 2009
Posts: 8
Reputation: mcco1 is an unknown quantity at this point 
Solved Threads: 0
mcco1 mcco1 is offline Offline
Newbie Poster

Re: Int assigning problem

 
0
  #3
Aug 19th, 2009
Originally Posted by Laiq Ahmed View Post
What are the values of RECT members?
http://msdn.microsoft.com/en-us/libr...97(VS.85).aspx
  1. typedef struct _RECT {
  2. LONG left;
  3. LONG top;
  4. LONG right;
  5. LONG bottom;
  6. }RECT, *PRECT;

The values are numbers
Reply With Quote Quick reply to this message  
Join Date: Jun 2006
Posts: 147
Reputation: Laiq Ahmed will become famous soon enough Laiq Ahmed will become famous soon enough 
Solved Threads: 20
Laiq Ahmed Laiq Ahmed is offline Offline
Junior Poster

Re: Int assigning problem

 
0
  #4
Aug 19th, 2009
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. .
Reply With Quote Quick reply to this message  
Join Date: Jan 2009
Posts: 303
Reputation: JasonHippy is a jewel in the rough JasonHippy is a jewel in the rough JasonHippy is a jewel in the rough JasonHippy is a jewel in the rough 
Solved Threads: 52
JasonHippy's Avatar
JasonHippy JasonHippy is offline Offline
Posting Whiz

Re: Int assigning problem

 
1
  #5
Aug 19th, 2009
Originally Posted by mcco1 View Post
I have this piece of code
  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.
There are 10 types of people in this world.....
Those who understand binary .....
And those who don't!
Reply With Quote Quick reply to this message  
Join Date: Mar 2009
Posts: 139
Reputation: MrSpigot is on a distinguished road 
Solved Threads: 33
MrSpigot's Avatar
MrSpigot MrSpigot is offline Offline
Junior Poster

Re: Int assigning problem

 
1
  #6
Aug 19th, 2009
Originally Posted by JasonHippy View Post
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.
Reply With Quote Quick reply to this message  
Join Date: Dec 2008
Posts: 1,122
Reputation: firstPerson is just really nice firstPerson is just really nice firstPerson is just really nice firstPerson is just really nice 
Solved Threads: 143
firstPerson's Avatar
firstPerson firstPerson is offline Offline
Veteran Poster

Re: Int assigning problem

 
0
  #7
Aug 19th, 2009
Try
::px = ((rect.right - rect.left) / 2) - 60;

I am assuming there is a local variable hiding the global.
I give up! 
1) What word becomes shorter if you add a letter to it? [ Solved by : niek_e ]
2) What does this sequence  equal to :  (.5u - .5a)(.5u-.5b)(.5u-.5c) ...
3) What is the 123456789 prime numer?
Reply With Quote Quick reply to this message  
Join Date: Aug 2009
Posts: 2
Reputation: Benamar is an unknown quantity at this point 
Solved Threads: 1
Benamar Benamar is offline Offline
Newbie Poster

Re: Int assigning problem

 
0
  #8
Aug 19th, 2009
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.
Reply With Quote Quick reply to this message  
Join Date: Aug 2009
Posts: 2
Reputation: Benamar is an unknown quantity at this point 
Solved Threads: 1
Benamar Benamar is offline Offline
Newbie Poster

Re: Int assigning problem

 
0
  #9
Aug 19th, 2009
Sorry the global variable is hidden not cached
Reply With Quote Quick reply to this message  
Join Date: Jul 2009
Posts: 8
Reputation: mcco1 is an unknown quantity at this point 
Solved Threads: 0
mcco1 mcco1 is offline Offline
Newbie Poster

Re: Int assigning problem

 
0
  #10
Aug 19th, 2009
Originally Posted by JasonHippy View Post
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.
Reply With Quote Quick reply to this message  
Reply

This thread has been marked solved.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC