C programming hint

Yogesh K Gupta gupta at asgb.UUCP
Tue Jul 16 05:32:09 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.

Unfortunately, strncpy uses the "loop" that you are trying to avoid.  It
also makes a redundant (in this case) check for the null character each time
through the loop that should be avoided.
-- 
Yogesh Gupta                           Advanced Systems Group,
{sdcrdcf, sdcsvax}!bmcg!asgb!gupta     Burroughs Corp., Boulder, CO.
--------------------------------------------------------------------
	All opinions contained in this message are my own and do not
	reflect those of my employer or the plant on my desk.



More information about the Comp.lang.c mailing list