subscripting constant arrays

Gordon Moffett gam at proper.UUCP
Thu Jan 12 13:36:23 AEST 1984


Recently I came across the following code fragment:

	char *cp, ch[10];
	unsigned u;
	.
	.
	for (cp = ch; u != 0; u >>=4) {
		*cp++ = "0123456789ABCDEF"[u & 0xF];
	}

Shocking isn't it?  But the subscripting of the constant array is
legal K&R syntax (see sec 7.1), so this does work on all valid compilers.

As a stylistic note, I would suggest putting the string in parentheses
to emphasize its use as an expression:

		*cp++ = ("0123456789ABCDEF")[u & 0xF];

This made it clearer to me, anyway (former APL hacker...).



More information about the Comp.lang.c mailing list