C programming hint

Stew Rubenstein stew at harvard.ARPA
Thu Jul 11 13:06:19 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.
>Here is the method I used:
>
>	char blanks[SIZE];	/* declare array of SIZE elements */
>	
>	blanks[0] = ' ';	/* initialize 1st element */
>
>	strncpy(blanks + 1, blanks, SIZE - 1);	/* initialize entire array */
>		   ^^^       ^^^       ^^^
>		    |         |         |
>		destination  source   how many characters to copy 
>
>The trick is to use strncpy in an almost recursive way.
>Well, I hope you found this useful.

This is not portable.  There is no guarantee that strncpy() copies
one char at a time in forward order.  If it is implemented using the
VAX MOVC3 instruction, for example, the overlap of the source and
destination strings does NOT affect the result!

Stew



More information about the Comp.lang.c mailing list