hardcoded constants
Vincent C. Hatem
vch at attibr.UUCP
Fri Dec 23 06:30:39 AEST 1988
In article <1988Dec21.133910.23182 at ateng.ateng.com>, chip at ateng.ateng.com (Chip Salzenberg) writes:
] According to henry at utzoo.uucp (Henry Spencer):
] >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,
] >but about things like (in a function to concatenate two strings with a
] >'/' in between):
] >
] > foo = malloc(strlen(a)+strlen(b)+2); /* 2 for '/' '\0' */
]
] Not hard:
]
] foo = malloc(strlen(a)+sizeof("/")+strlen(b));
]
] --
] Chip Salzenberg <chip at ateng.com> or <uunet!ateng!chip>
] A T Engineering Me? Speak for my company? Surely you jest!
] "It's no good. They're tapping the lines."
Right, chip.
Last I heard sizeof("/") == sizeof(char *) - which is almost never 2, and
NEVER portable.
How about the more accurate:
foo = malloc(strlen(a)+strlen(b)+(2*sizeof(char)));
--
Vincent C. Hatem | att ---->\ (available from any
AT&T International | ulysses ->\ Action Central site)
International Operations Technical Support | bellcore ->\___ !attibr!vch
1200 Mt Kemble Ave, Basking Ridge, NJ 07920 | (201) 953-8030
More information about the Comp.lang.c
mailing list