Casting Pointers

bpl at lzpfd.UUCP bpl at lzpfd.UUCP
Thu Jan 26 03:33:49 AEST 1984


Recently I came across a problem in  C  which  I  wonder  if
somebody  out  there  in  netland  can  help me with.  I was
trying to copy a string on a 68000 in C, and to make it fast
I was trying (where possible) to do the transfers a longword
at a time.

No Problem, I said to myself, its easy: just  cast  the  two
pointers to be long pointers.  So I came up with:

	     *(long *)to++ = *(long *)from++;

Then I went to look at  page  49  of  the  White  Book,  and
realized  that  the  ++  and  casts assosciate from right to
left, which means it would only increment  the  pointers  by
one for each time through.

Well, still no problem, I thought: that's what brackets  are
for.  So I changed the line to:

	   *((long *)to)++ = *((long *)from++;

I decided to test it on the VAX first, rather than  download
it  to  the  68000.  So I tried to compile it with the 5.0 C
compiler.  "illegal lhs of assignment", it said.  So I tried
it  on  the  68000  cross compiler, also based on PCC.  This
time it compiled prefectly, and ran no problem.

The cause of the difference is the treatment of casting: the
5.0  VAX  C  compiler  assumes  that the result of casting a
pointer of one type to a pointer of another type is  not  an
lvalue, even though the original pointer was an lvalue.  The
68000 cross-compiler assumes that the  result  is  still  an
lvalue.

I have read the White Book, and it leaves me as confused  as
ever.   Can  anybody  out  there  tell  me which Compiler is
correct, or is it an  ambiguity  which  leaves  you  to  the
tender mercies of you local PCC hacker.

To avoid flames: yes,  I  know  what  I  am  doing  is  non-
portable.   I  am  not  trying to write portable code.  I am
trying to write a fast copy for a  68000.   I  have  checked
that the pointers are really word-aligned.

				Brendan Lynch
				(pegasus!lzpfd!bpl)



More information about the Comp.lang.c mailing list