Solved

understanding how union works

Posted on 2004-04-23
2
293 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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

[Live Webinar] The Cloud Skills Gap

As Cloud technologies come of age, business leaders grapple with the impact it has on their team's skills and the gap associated with the use of a cloud platform.

Join experts from 451 Research and Concerto Cloud Services on July 27th where we will examine fact and fiction.

Question has a verified solution.

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

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…
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 opening and writing to files in the C programming language.
The goal of this video is to provide viewers with basic examples to understand how to use strings and some functions related to them in the C programming language.
Suggested Courses

636 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