Article View: comp.std.c
Article #3456Re: (no subject) [cast expression as an lvalue]
From: Michael Smith
Date: Tue, 10 Oct 1995 16:16
Date: Tue, 10 Oct 1995 16:16
54 lines
2669 bytes
2669 bytes
Life is hard... and then you die. (Ronald.Tschalaer@psi.ch) wrote:
: Morgan Herrington wrote:
: >
: > Daniel Wood <dwood> writes:
: >
: > > The following:
: > > ((char *)src)++; /* src is an (int *) */
: The upshot is, why are you doing this?
Looks like some old code I inherited a while back. The code was part of
an expression compiler/evaluator. It used a character array as a stack
during compilation and was full of code like:
switch (argtype) {
case TYPE_INT:
*((int *)stack)++ = *((int *)argptr)++;
break;
case TYPE_DOUBLE:
*((double *)stack)++ = *((double *)argptr)++;
break;
/* etc ..... */
}
I don't swear that this is the exact syntax (it was a while ago) but it
gives the general idea.
I **THINK** that this was considered legal using K&R1 style C. It's not
legal as ANSI C but many compilers let you get away with it. When you've
got 2000 or so lines of poorly documented spagetti code using such
constructs and the code works flawlessly then it is a tough decision to
order a re-write.
We used the code with no problems under DOS and Windows 3.x. When we
moved it to a Sun Sparc it gave about 300 lines of warnings but ran fine
(as long as it was compiled with a switch to allow mis-aligned data).
When we moved to a SGI Iris (IRIX 4.x) we hit a brick wall. The compiler
gave fatal errors for the expressions as LVALUES and didn't support
mis-aligned data. Came time for a re-write.
When 64KB was a lot of memory, an XT was fast and a Vax 11/750 was a
monster, people used to write some ugly code. These days we can afford a
few extra bytes to make code readable.
#####################################################################
Michael Smith msmith@mpx.com.au
Emmenjay Consulting
Making computers work for you, not the other way around.
PO Box 909 Ph 018 240 704
Kensington 2033
AUSTRALIA
#####################################################################
Technical Details
Message-ID:
<45e68p$t78@inferno.mpx.com.au>Path:
arc1-ca.pugleaf.net!inferno.mpx.com.au!jolt!msmithReferences:
<4578r1$jub@news.informix.com> <458bh8$5jm@newsworthy.west.sun.com> <3077F1E7.41C67EA6@psi.ch>Show Full Headers
Complete Article Headers
Path: arc1-ca.pugleaf.net!inferno.mpx.com.au!jolt!msmith From: msmith@jolt.mpx.com.au (Michael Smith) Newsgroups: comp.std.c,comp.lang.c,comp.unix.solaris Subject: Re: (no subject) [cast expression as an lvalue] Followup-To: comp.std.c,comp.lang.c,comp.unix.solaris Date: 10 Oct 1995 16:16:25 GMT Organization: Microplex Pty Ltd Distribution: inet Message-ID: <45e68p$t78@inferno.mpx.com.au> References: <4578r1$jub@news.informix.com> <458bh8$5jm@newsworthy.west.sun.com> <3077F1E7.41C67EA6@psi.ch> NNTP-Posting-Host: jolt.mpx.com.au X-Newsreader: TIN [version 1.2 PL2]