When is a cast not a cast?

Jeff Erickson krazy at claris.com
Tue May 2 22:37:01 AEST 1989


>From article <2747 at buengc.BU.EDU>, by bph at buengc.BU.EDU (Blair P. Houghton):
> 1   main()
> 2   {
> 3	char *c;
> 4	char *p;
> 5	int i;
> 6	...
> 7	c = "somestring";  /* Nothing fancy, null-terminated. */
> 8	i = 4;  /* For example. */
> 9	...
> 10 	p = (c + (char *) i);  /* More trouble than it's worth... */
> 11	...
> 12  }
> 
> wherupon both the lint(1) and cc(1) in my Ultrix 2.2 piped-up with
> warnings that the 'operands of + have incompatible types' on line 10...

Well, first of all, the only arithmetic operations allowed on pointers
are pointer+integer, integer+pointer, pointer-integer, and pointer-pointer.
Pointer+pointer is illegal, and therefore, "operands of + have incompatible
types".

Secondly, I believe what you *want* on line 10 is

				p = c + i;

This is equivalent to "p = &(c[i]);".  It sets p equal to a pointer to the 
i'th char after c.


-- 
Jeff Erickson       Claris Corporation  | Birdie, birdie, in the sky,
408/987-7309      Applelink: Erickson4  |   Why'd you do that in my eye?
krazy at claris.com     ames!claris!krazy  | I won't fret, and I won't cry.
       "I'm a heppy, heppy ket!"        |   I'm just glad that cows don't fly.



More information about the Comp.lang.c mailing list