Solved

understanding how union works

Posted on 2004-04-23
2
286 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
Comment Utility
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
Comment Utility
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

How to improve team productivity

Quip adds documents, spreadsheets, and tasklists to your Slack experience
- Elevate ideas to Quip docs
- Share Quip docs in Slack
- Get notified of changes to your docs
- Available on iOS/Android/Desktop/Web
- Online/Offline

Join & Write a Comment

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…
This is a short and sweet, but (hopefully) to the point article. There seems to be some fundamental misunderstanding about the function prototype for the "main" function in C and C++, more specifically what type this function should return. I see so…
The goal of this video is to provide viewers with basic examples to understand opening and reading files 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.

772 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

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now