subscripting constant arrays - (nf)

ucbesvax.turner at ucbcad.UUCP ucbesvax.turner at ucbcad.UUCP
Sun Jan 15 23:08:06 AEST 1984


#R:proper:-83400:ucbesvax:4800032:000:678
ucbesvax!turner    Jan 14 12:23:00 1984

re: subscripting constant arrays
.
Not only can you say "0123"[i], and the like, but sizeof ("0123") reliably
returns 5 (not 4--think.)  This is even useful, occasionally--as when
you wish to test if the first few chars of a string match some literal
constant string.  Example:

#define MATCH(s,sc) (strncmp (s, sc, sizeof(sc)-1) == 0)

	    if (MATCH (s, "add")) ...
then becomes
	    if (strncmp (s, "add", 3) == 0)

Here, of course, if sc is not a literal or constant string, sizeof (sc)
becomes sizeof (char *).  The ideal MATCH macro would detect this and
generate cc-warning code instead, but I'm not quite sure how to do that.
---
Michael Turner (ucbvax!ucbesvax.turner)



More information about the Comp.lang.c mailing list