Path: utzoo!utgpu!jarvis.csri.toronto.edu!mailrus!ncar!ico!ism780c!haddock!suitti From: suitti@haddock.ima.isc.com (Stephen Uitti) Newsgroups: comp.lang.c Subject: Re: When is a cast not a cast? Message-ID: <12934@haddock.ima.isc.com> Date: 2 May 89 23:38:51 GMT References: <2747@buengc.BU.EDU> Reply-To: suitti@haddock.ima.isc.com (Stephen Uitti) Organization: Interactive Systems, Boston Lines: 27 In article <2747@buengc.BU.EDU> bph@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.