hardcoded constants

Guy Harris guy at auspex.UUCP
Sat Dec 17 15:02:13 AEST 1988


>How about sizeof "/"? Or does that return sizeof(char *)?

No, it returns the number of bytes in the anonymous character array that
"/" is.  K&R First Edition says "When applied to an array, the result is
the number of bytes in the array", and also 

	A string is a sequence of character surrounded by double quotes,
	as in "...".  A string has type "array of characters"...

so "/" is an array, and "sizeof", when applied to it, returns the number
of characters in it.  The dpANS says much the same thing.

Unfortunately or fortunately, depending on how you look at it, 'sizeof
"/"' is 2, since the array in question has *two* characters - the '/'
and the '\0' at the end.  This means that

	strlen(a) + sizeof "/" + strlen(b)

happens to be the minimum number of characters that must be in the array
"buf" to make

	strcpy(buf, a);
	strcat(buf, "/");
	strcat(buf, b);

work, since it counts both the "/" added by the first "strcat" and the
null left at the end; however

	strlen(a) + sizeof "/" + strlen(b) + sizeof "/" + strlen(c)

is one more than the minimum number of characters that must be in the
array "buf" to make

	strcpy(buf, a);
	strcat(buf, "/");
	strcat(buf, b);
	strcat(buf, "/");
	strcat(buf, c);

work.  In this particular case it may not be worth worrying about since
it's only one character....



More information about the Comp.lang.c mailing list