954,492 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

Int assigning problem

I have this piece of code

// some function
int px;
void SomeFunction( void *dummy )
{
	...

	RECT rect;
	GetWindowRect(hWnd, &rect);
	px = ((rect.right - rect.left) / 2) - 60;

	...

	// Terminate thread
	_endthread();
}


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

mcco1
Newbie Poster
10 posts since Jul 2009
Reputation Points: 10
Solved Threads: 0
 

What are the values of RECT members?

Laiq Ahmed
Junior Poster
148 posts since Jun 2006
Reputation Points: 113
Solved Threads: 20
 
What are the values of RECT members?

http://msdn.microsoft.com/en-us/library/dd162897(VS.85).aspx

typedef struct _RECT {
  LONG left;
  LONG top;
  LONG right;
  LONG bottom;
}RECT, *PRECT;


The values are numbers :)

mcco1
Newbie Poster
10 posts since Jul 2009
Reputation Points: 10
Solved Threads: 0
 

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. :).

Laiq Ahmed
Junior Poster
148 posts since Jun 2006
Reputation Points: 113
Solved Threads: 20
 

I have this piece of code

// some function
int px;
void SomeFunction( void *dummy )
{
	...

	RECT rect;
	GetWindowRect(hWnd, &rect);
	px = ((rect.right - rect.left) / 2) - 60;

	...

	// Terminate thread
	_endthread();
}

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.

JasonHippy
Master Poster
772 posts since Jan 2009
Reputation Points: 590
Solved Threads: 125
 

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.

MrSpigot
Junior Poster
158 posts since Mar 2009
Reputation Points: 76
Solved Threads: 40
 

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

I am assuming there is a local variable hiding the global.

firstPerson
Senior Poster
3,923 posts since Dec 2008
Reputation Points: 841
Solved Threads: 608
 

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.

Benamar
Newbie Poster
2 posts since Aug 2009
Reputation Points: 10
Solved Threads: 1
 

Sorry the global variable is hidden not cached

Benamar
Newbie Poster
2 posts since Aug 2009
Reputation Points: 10
Solved Threads: 1
 

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.

mcco1
Newbie Poster
10 posts since Jul 2009
Reputation Points: 10
Solved Threads: 0
 
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.

Of course...Sorry, another window-licker moment of mine. I think I was confusing long for double, I saw long and my brain was thinking double....What a dumbass! heh heh!

I think some extra coffee is in order!

Jas.

JasonHippy
Master Poster
772 posts since Jan 2009
Reputation Points: 590
Solved Threads: 125
 

This question has already been solved

Post: Markdown Syntax: Formatting Help
You