When is a cast not a cast?
Stephen Uitti
suitti at haddock.ima.isc.com
Wed May 3 09:38:51 AEST 1989
In article <2747 at buengc.BU.EDU> bph at buengc.bu.edu (Blair P. Houghton) writes:
>I wanted to make all the types match up, so I was using casts
>wherever a promotion might be non-obvious. In particular, I fell for:
>3 char *c;
>5 int i;
>7 c = "somestring"; /* Nothing fancy, null-terminated. */
>8 i = 4; /* For example. */
>10 p = (c + (char *) i); /* More trouble than it's worth... */
>wherupon cc(1) said that the 'operands of + have incompatible types'
>
>Now, who is having the more serious problem with (reduntantly?) casting
>i to be a char * before this addition: me, or the programmming tools
>under Ultrix version 2.2?
It isn't even redundant. It means the wrong thing. Casts tend not
to be redundant. Sometimes they are used for "portability". Not here.
A better example is
p = "foobar" + "barfoo"; /* what does this mean? */
The line
p = c + i; /* means p = c[i] or p = i[c] or p = i + c */
does something useful.
K&R says something about what arithmetic can be done with pointers.
I'd quote from the bible were it required.
Stephen.
More information about the Comp.lang.c
mailing list