C programming hint

kps at teddy.UUCP kps at teddy.UUCP
Thu Jul 11 05:04:06 AEST 1985


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.



More information about the Comp.lang.c mailing list