Solved

understanding how union works

Posted on 2004-04-23
2
291 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 20 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 30 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

Salesforce Made Easy to Use

On-screen guidance at the moment of need enables you & your employees to focus on the core, you can now boost your adoption rates swiftly and simply with one easy tool.

Question has a verified solution.

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

Suggested Solutions

Have you thought about creating an iPhone application (app), but didn't even know where to get started? Here's how: ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ Important pre-programming comments: I’ve never tri…
Summary: This tutorial covers some basics of pointer, pointer arithmetic and function pointer. What is a pointer: A pointer is a variable which holds an address. This address might be address of another variable/address of devices/address of fu…
The goal of this video is to provide viewers with basic examples to understand how to create, access, and change arrays in the C programming language.
The goal of this video is to provide viewers with basic examples to understand and use conditional statements in the C programming language.

821 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