Solved

understanding how union works

Posted on 2004-04-23
2
289 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

What is SQL Server and how does it work?

The purpose of this paper is to provide you background on SQL Server. It’s your self-study guide for learning fundamentals. It includes both the history of SQL and its technical basics. Concepts and definitions will form the solid foundation of your future DBA expertise.

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…
Preface I don't like visual development tools that are supposed to write a program for me. Even if it is Xcode and I can use Interface Builder. Yes, it is a perfect tool and has helped me a lot, mainly, in the beginning, when my programs were small…
The goal of this video is to provide viewers with basic examples to understand and use structures in the C programming language.
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.

773 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