C programming hint

johnston at uiucdcsb.Uiuc.ARPA johnston at uiucdcsb.Uiuc.ARPA
Fri Jul 19 00:04:00 AEST 1985


/* Written  2:04 pm  Jul 10, 1985 by kps at teddy.UUCP in uiucdcsb:net.lang.c */
/* ---------- "C programming hint" ---------- */
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.
/* End of text from uiucdcsb:net.lang.c */



More information about the Comp.lang.c mailing list