C programming hint

Walter Bright bright at dataio.UUCP
Fri Jul 12 19:41:25 AEST 1985


In article <899 at teddy.UUCP> kps at teddy.UUCP (Kesavan P. Srinivasan) writes:
>I found a way to initialize an array of characters without using a loop.
>	char blanks[SIZE];	/* declare array of SIZE elements */
>	
>	blanks[0] = ' ';	/* initialize 1st element */
>
>	strncpy(blanks + 1, blanks, SIZE - 1);	/* initialize entire array */
>
>The trick is to use strncpy in an almost recursive way.

	This will fail if strncpy() copies starting from the end of the source
string, rather than from the beginning. You are depending on an undefined
side effect of your implementation of strncpy().
	Try using memset():

	memset(blanks,' ',SIZE-1);



More information about the Comp.lang.c mailing list