Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

understanding how union works

Posted on 2004-04-23
2
Medium Priority
?
295 Views
Last Modified: 2010-04-15
Ok here is a sample code that i dont understand. I use turboc compiler.

unsigned long BaseAddress1=0xC8000L;    //Default Base Address, transmitter
unsigned long BaseAddress2=0xC8100L;    //Default Base Address, receiverunsigned char far* NormAddress1;       //Base address in form SEGM:ADDR
unsigned char far* NormAddress2;       //Base address in form SEGM:ADDR
unsigned char far* NormAddress1;       //Base address in form SEGM:ADDR

union UAddrLng
{
      unsigned char far* Addr;
      unsigned long Numb;
}UAL;


unsigned char far* GetAddress ( unsigned long Ad)
{
      UAL.Numb=Ad;
      Ad&=0xF;
      UAL.Numb&=0xFFFF0l;
      UAL.Numb=(UAL.Numb<<12)|Ad;
      return UAL.Addr;
}

int main (int argc, const char *argv[])

{
      NormAddress1=GetAddress(BaseAddress1);
                printf("NormAddress1= %x",NormAddress1);
      NormAddress2=GetAddress(BaseAddress2);
}

I dont know how to display the unsigned char far* and unsigned long. Also dont understand how unions work. Tried looking online tutorial but got confused. So what does the function GetAddress  return?? thanks in advance.
0
Comment
Question by:rajiv11
2 Comments
 
LVL 22

Assisted Solution

by:NovaDenizen
NovaDenizen earned 80 total points
ID: 10903824
To display unsigned char far *:  use "%p" in your printf format string.
To display unsigned long:  use "%lu" in your printf format string.

The basic idea of the union is that all of its members are different ways of looking at the same bytes.  In your example, the Numb field looks at the value in the context of an unsigned long.  The Addr field looks at the same few bytes, but treats them as an unsigned char far *.  If you added a 'char Bytes[4];' line to the union definition, you could look at each of those same bytes individually using the Bytes array field.  If you added a 'float Fval;' line, you could use Fval to get a (totally nonsensical) reinterpretation of those same bytes as a floating point number.

0
 
LVL 22

Accepted Solution

by:
grg99 earned 120 total points
ID: 10904774
union UAddrLng
{
     unsigned char far* Addr;
     unsigned long Numb;
}UAL;


//The above just says that we can look at the same variable as a 32-bit address "addr"
//or as a 32-bit unsigned number "Numb"


unsigned char far* GetAddress ( unsigned long Ad)
{
     UAL.Numb = Ad;    // put the c8000 into UAL
     Ad &=  0xF;          // toss everything but bottom 4 bits of Ad
     UAL.Numb &= 0xFFFF0l;   // Remove bottom 4 bits of UAL
     UAL.Numb = (UAL.Numb<<12) |  Ad;  // put the high 12 bits into the high word of UAL
     return UAL.Addr;   // and the bototm 4 bits into the low word.
}


It looks like this routine takes a "flat" 32-bit address and makes a real-mode pointer out of it.

The whole rigamarole could be simplified into :

unsigned char far* NormAddress2 = MK_FP( 0xC800,0000);       //Base address in form SEGM:ADDR
unsigned char far* NormAddress1 = MK_FP( 0xC810,0000);       //Base address in form SEGM:ADDR

0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

An Outlet in Cocoa is a persistent reference to a GUI control; it connects a property (a variable) to a control.  For example, it is common to create an Outlet for the text field GUI control and change the text that appears in this field via that Ou…
Windows programmers of the C/C++ variety, how many of you realise that since Window 9x Microsoft has been lying to you about what constitutes Unicode (http://en.wikipedia.org/wiki/Unicode)? They will have you believe that Unicode requires you to use…
The goal of this video is to provide viewers with basic examples to understand and use structures in the C programming language.
Video by: Grant
The goal of this video is to provide viewers with basic examples to understand and use while-loops in the C programming language.

783 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question