Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

checking variable type size... 1

Status
Not open for further replies.

dtvonly

Technical User
Jan 3, 2008
8
US
Hi. Please see the short sample below:

unsigned int var1;

var1 = 0x0001; // 16-bit data to device A
var1 = 0x01; // 8-bit data to device B

Send_Data(var1);

my objective: Send_Data subroutine function depends on the data length of var1. How can this be done?

Thank you for your help.
 
The size of the variable depends solely on [tt]sizeof(unsigned int)[/tt], not how many leading zeros you have in your assignment.

[tt]var = 0x00000001;[/tt]
would be just the same.


--
If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
 
0x01 is just a number. There is no way of telling whether it takes 1, 2, 4 or 8 bytes. Why not just tell the sending routine how many bytes you wish to send?
 
Hi. Thank you for your reply. Let's try this:

var1 = 0x1001; // 16-bit data to device A
var1 = 0x11; // 8-bit data to device B
Send_Data(var1);
 
Hi xwb. I have already done this. I was just wondering if there was any other way. Thank you for your help.
 
Well then Send_Data() is going to have to look at var1, then decide to say send only 1 byte if var1 is <= 255.



--
If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
 
if ( (var1 & 0xFF) == var1 )
{
// var1 has 8-bit data.
}
else if ( (var1 & 0xFFFF) == var1 )
{
// var1 has 16-bit data.
}
else if ( (var1 & 0xFFFFFFFF) == var1 )
{
// var1 has 32-bit data.
}
 
Can't see how that solves the problem - if you want to send 1 as 16 bits, it will send it as 8 bits.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top