[net.lang.c] A diagnostic "gotcha"

kanner@tymix.UUCP (Herb Kanner) (02/06/86)

A line of C code that looked like:

	node_mem[*dotp] = val & 0xff;

produced the diagonistic "operands of + have incompatible types."  The
error was that *dotp was not of type int, and a[b] being a euphemism for
*(a + b), it complained about the operands of addition.
-- 
Herb Kanner
McDonnell Douglas (TYMNET)
...!hplabs!oliveb!tymix!kanner

kanner@tymix.UUCP (Herb Kanner) (02/11/86)

In article <648@tymix.UUCP> kanner@tymix.UUCP (Herb Kanner) writes:
>A line of C code that looked like:
>
>	node_mem[*dotp] = val & 0xff;
>
>produced the diagonistic "operands of + have incompatible types."  The
>error was that *dotp was not of type int, and a[b] being a euphemism for
>*(a + b), it complained about the operands of addition.

I received the following note:

	Subject: Re: A diagnostic "gotcha"
	Newsgroups: net.lang.c
	In-Reply-To: <648@tymix.UUCP>
	Organization: Hadron, Inc., Fairfax, VA
	Status: RO

	Wouldst be so good as to tell the Net what version of C, and
	what machine?   They're all different, to some degree, in the
	issued diagnostics.
	-- 

		Joe Yao		hadron!jsdy@seismo.{CSS.GOV,ARPA,UUCP}

Sorry about that.  It is the standard C compiler on 4.2BSD running on a 
VAX 750.
-- 
Herb Kanner
McDonnell Douglas (TYMNET)
...!hplabs!oliveb!tymix!kanner

chris@umcp-cs.UUCP (Chris Torek) (02/13/86)

In article <659@tymix.UUCP> kanner@tymix.UUCP (Herb Kanner) writes:
>A line of C code that looked like:
>
>	node_mem[*dotp] = val & 0xff;
>
>produced the diagonistic "operands of + have incompatible types."  The
>error was that *dotp was not of type int, and a[b] being a euphemism for
>*(a + b), it complained about the operands of addition.

At a guess, I would say that `dotp' was declared as a pointer to
an enumerated type.  `char' and `short' are promoted to `int' in
addition expressions.  `enum's being what they are (half in and
half out of integer space at the moment), the 4.2 and 4.3 BSD C
compilers will not allow them to be added to pointers.  The ANSI
standard should clear this up.

In the meantime, a cast suffices to make the compiler accept the code:

	node_mem[(int) *dotp] = val & 0xff;

---though to my mind using enumerated types as indicies is `treading
on thin ice' (I have no explanation as to why I feel this way; I
just do).
-- 
In-Real-Life: Chris Torek, Univ of MD Comp Sci Dept (+1 301 454 1415)
UUCP:	seismo!umcp-cs!chris
CSNet:	chris@umcp-cs		ARPA:	chris@mimsy.umd.edu