comments (was Re: hardcoded constants)

David Goodenough dg at lakart.UUCP
Fri Jan 6 01:30:24 AEST 1989


>From article <34 at datcon.UUCP>, by sar at datcon.UUCP (Simon A Reap):
K In article <1988Dec21.133910.23182 at ateng.ateng.com> chip at ateng.ateng.com
i 						    (Chip Salzenberg) writes:
s>According to henry at utzoo.uucp (Henry Spencer):
s>>Trouble is, often it's almost impossible to devise a meaningful name.
 >>I'm not talking about hard-coding things like choice of control characters,
a>>but about things like (in a function to concatenate two strings with a
s>>'/' in between):
s>>	foo = malloc(strlen(a)+strlen(b)+2);	/* 2 for '/' '\0' */
 >Not hard:
i>	foo = malloc(strlen(a)+sizeof("/")+strlen(b));
n 
e Ah, but if we want to concatenate more strings, don't we need something
w like....
s 
  #define TO_CAT_3_STRINGS (-1)
> #define TO_CAT_4_STRINGS (-2)
> 	foo = malloc(strlen(a)+sizeof("/")+strlen(b)+sizeof("/")+
> 			strlen(c)+TO_CAT_3_STRINGS);
> 	foo = malloc(strlen(a)+sizeof("/")+strlen(b)+sizeof("/")+
> 			strlen(b)+sizeof("/")+strlen(c)+TO_CAT_4_STRINGS);

Forgive my ignorance, but isn't this what comments were invented for?

	foo = malloc(strlen(a)+strlen(b)+2);	/* 2 for '/' '\0' */
						^^^^^^^^^^^^^^^^^^^^

or:

	foo = malloc(strlen(a) + strlen(b) + strlen(c) + 3);
						/* 3 for '/', '/', '\0' */
-- 
	dg at lakart.UUCP - David Goodenough		+---+
							| +-+-+
	....... !harvard!xait!lakart!dg			+-+-+ |
AKA:	dg%lakart.uucp at xait.xerox.com		  	  +---+



More information about the Comp.lang.c mailing list