[comp.lang.c] Pointers and addresses

shepherd@mentor.cc.purdue.edu (Mike Shepherd) (10/30/90)

Is there a way to change an integer into an address location, vice versa, an
address location into an integer?

I keep getting illegal indirection & illegal pointer/integer comb errors.

Is this possible?

Thanks,
Mike
-- 
|   Mike Shepherd	(shepherd@mentor.cc.purdue.edu)			      |
|-----------------------------------------------------------------------------|
| Sometimes I think the surest sign that intelligent life exists elsewhere in |
| the universe is that none of it has tried to contact us.    --Calvin of C&H |

ok@goanna.cs.rmit.oz.au (Richard A. O'Keefe) (10/30/90)

In article <15831@mentor.cc.purdue.edu>,
  shepherd@mentor.cc.purdue.edu (Mike Shepherd) writes:
> Is there a way to change an integer into an address location, vice versa, an
> address location into an integer?

What on earth is an address location?  Do you mean a pointer?
How about an example of what you want to do?

Bear in mind that pointers and integers are different sizes on some
machines (e.g. Primes with 32-bit integers and 48-bit pointers, PCs
with 16-bit "int" and 32-bit "far pointer") and that even on machines
where they are the same size not all integers correspond to valid
addresses (e.g. 370/XA).  You really do need to tell us what you are
trying to do before we can give you advice worth having.

> I keep getting illegal indirection & illegal pointer/integer comb errors.
> Is this possible?

If it happens, it's possible.
Why don't you show us a _small_ example of the code you're trying to
write and what goes wrong with it, so that we can help?

(I thought Purdue was supposed to be one of the really good places for
Comp Sci.  Surely there must be _someone_ there who can give you face-
to-face help?  That's bound to be better than the Net.)
-- 
The problem about real life is that moving one's knight to QB3
may always be replied to with a lob across the net.  --Alasdair Macintyre.

shepherd@mentor.cc.purdue.edu (Mike Shepherd) (10/30/90)

In article <4139@goanna.cs.rmit.oz.au> ok@goanna.cs.rmit.oz.au (Richard A. O'Keefe) writes:
>In article <15831@mentor.cc.purdue.edu>,
>  shepherd@mentor.cc.purdue.edu (Mike Shepherd) writes:
>> Is there a way to change an integer into an address location, vice versa, an
>> address location into an integer?
>
>What on earth is an address location?  Do you mean a pointer?
>How about an example of what you want to do?
>
>(I thought Purdue was supposed to be one of the really good places for
>Comp Sci.  Surely there must be _someone_ there who can give you face-
>to-face help?  That's bound to be better than the Net.)

	Well, someone apparently did know what I was talking about because
someone DID give me help from the Net.  And it worked out very
well...here is what they wrote (and in a very nice and creative way if I
might add) in case others of you are interested. And thanks again Xing! ...

----------------------------------------------------------

Subject: Re: Pointers and addresses
Newsgroups: comp.lang.c
In-Reply-To: <15831@mentor.cc.purdue.edu>
Organization: Marquette University - Milwaukee, Wisconsin
Message-Id: <9010292031.AA08370@compsys.mu.edu>
Date: 29 Oct 90 20:31:59 CST (Mon)
From: wuxing@comp.mscs.mu.edu (Xing Wu)
Status: OR

In article <15831@mentor.cc.purdue.edu> you write:
>Is there a way to change an integer into an address location, vice versa, an
>address location into an integer?
>I keep getting illegal indirection & illegal pointer/integer comb errors.
>Is this possible?

This is just the compiler's way of saying "You know that it's just 32 bits,
and I know that it's just 32 bits, but I'm clever enough to know that it's
a *different type* of 32 bits."

The thing to do is to use a "cast":

char *s;
int i;

i = (int)s;
s = (char *)i;

etc.

A cast is your way of saying "I'm clever enough to know that you're clever
enough to know that it's a different type of 32 bits, so in your face."

As I always say, "strong typing is for weak minds."

----------------------------------------------------------

And thanks again Xing...it worked perfect!


Mike
-- 
|   Mike Shepherd	(shepherd@mentor.cc.purdue.edu)			      |
|-----------------------------------------------------------------------------|
| Sometimes I think the surest sign that intelligent life exists elsewhere in |
| the universe is that none of it has tried to contact us.    --Calvin of C&H |

ok@goanna.cs.rmit.oz.au (Richard A. O'Keefe) (10/30/90)

In article <15847@mentor.cc.purdue.edu>, shepherd@mentor.cc.purdue.edu (Mike Shepherd) writes:
> In article <4139@goanna.cs.rmit.oz.au> ok@goanna.cs.rmit.oz.au (Richard A. O'Keefe) writes:
> >In article <15831@mentor.cc.purdue.edu>,
> >  shepherd@mentor.cc.purdue.edu (Mike Shepherd) writes:
> >> Is there a way to change an integer into an address location, vice versa, an
> >> address location into an integer?

>ok> What on earth is an address location?  Do you mean a pointer?
>ok> How about an example of what you want to do?

> 	Well, someone apparently did know what I was talking about because
> someone DID give me help from the Net.  And it worked out very well...
" This is just the compiler's way of saying "You know that it's just 32 bits,"
" and I know that it's just 32 bits, but I'm clever enough to know that it's"
" a *different type* of 32 bits."
" The thing to do is to use a "cast": .."

I have to repeat the negative advice I gave in 4139@goanna.
This bit about "you know it's 32 bits and I know it's 32 bits"
is a disaster waiting to happen.  I gave examples of machines
where pointers aren't 32 bits or integers aren't 32 bits.
It isn't even the case that all pointers are the same size;
there are machines where "data" pointers are 1 word and "function"
pointers are 2 (or even more).  Even on machines where pointers
and ints _are_ the same size, casting may require runtime operations,
and casting some bit patterns from int to pointer may cause a trap.

The advice you were given is ok _if_ you are _consciously_ writing
non-portable code, or at any rate code portable only within a known
range of machines ("32-bit byte-addressed workstations running UNIX", say).
But that's why it was necessary to ask what you _really_ want to do.
Go ahead and make the assumption, but *write it down*, and be prepared
to have someone try to port your code to a PC and come screaming for blood.

I repeat:  how about an example of what you really want to do?
Sure, casts are the *obvious* way of hacking it, they're in every
elementary introduction to C.  But for your actual application,
there may be a more portable alternative.

-- 
The problem about real life is that moving one's knight to QB3
may always be replied to with a lob across the net.  --Alasdair Macintyre.