Assigning an address to an integer

Bruce Kitchin kitchin at hpavla.AVO.HP.COM
Thu Jun 7 00:44:02 AEST 1990


{
  int a,d;

  a=&d;
  bdos(1,a,1);
}

The problem is that &d is not of type int.  Others have suggested that you
say (int)&d.  This happens to make my blood boil.  I don't know how many
'portable' programs including this from GNU that I've tried to port to MSDOS
that made the assumption that sizeof(int) == sizeof(int *).  That is simply
not portable and on some machines such as 80x86 will only work in limited
cases.  According to K&R (page 210), "A pointer may be converted to any of
the integral types large enough to hold it.  Whether an int or long is
required is machine dependent."  Having wasted many hours trying to find all
of the places in 'portable' programs that this bad assumption has been made,
sizeof(int) == sizeof(int *), I have trouble with converting pointers to int's.

Having gotten that off my chest, let's look at your problem.  If I am making
the correct assumption, bdos looks like an MSDOS interface function.  The
only MSDOS functions that this can call are those that take a DS relative
address (ie., near address).  To use this function you must assure that the
program is compiled as a Small model program or that d is located in the defaultdata segment.  Unless the program is compiled for DS != SS, all stack allocated
variables are default data segment (all non-static local variables in your
functions, for example, and all function arguments).  If you are sure that
these assumptions are true then you can do something like:

  {
     int far *a;
     int d;
     a = &d
     bdos(1,FP_OFF(a),1);
   }

where FP_OFF is defined in dos.h.  Otherwise you should use the intdos function
in which you supply the segment registers as well as the ordinary registers
to the int 0x21 call.  Since this is a dos interface, you may want to see
if your compiler's library has a more specific dos call to do what you are
after.  For function 1, getche() suggests itself.  There are also a number
of functions supplied by MSC and Turbo-C for Dos specific interfaces.



More information about the Comp.lang.c mailing list