"handy.h"

BALDWIN mike at whuxl.UUCP
Wed Sep 4 04:28:57 AEST 1985


>I'm interested in coming up with an include file, say "handy.h",
>which declares a lot of commonly useful "stuff".

These can be "handy":

#define	NELEM(arr)	(sizeof (arr) / sizeof *(arr))
#define	LAST(arr)	((arr) + NELEM(arr))

e.g.,
	struct vegetable potato[] = {
		"burbank", 0.34,
		"new", 3.44,
		"hot", 0.12,
	};
then
	struct vegetable *pp;
	for (pp = potato; pp < LAST(potato); pp++)
or
	int i;
	for (i = 0; i < NELEM(potato); i++)

I also like the usual string stuff:

#define	EQUAL(a,b)	!strcmp(a,b)
#define	EQUALN(a,b,n)	!strncmp(a,b,n)
#define	PREFIX(a,b)	!strncmp(a,b,strlen(a))
#define	COPY(a,b)	strncpy(a,b,sizeof a)

-- 
						Michael Baldwin
						AT&T Bell Labs
						{at&t}!whuxl!mike



More information about the Comp.lang.c mailing list