Relay-Version: version B 2.10 5/3/83; site utzoo.UUCP Posting-Version: version B 2.10.2 9/18/84; site asgb.UUCP Path: utzoo!watmath!clyde!burl!ulysses!allegra!mit-eddie!think!harvard!seismo!hao!asgb!mah From: mah@asgb.UUCP (Mark Hamilton) Newsgroups: net.lang.c Subject: Re: C programming hint Message-ID: <730@asgb.UUCP> Date: Mon, 15-Jul-85 16:09:45 EDT Article-I.D.: asgb.730 Posted: Mon Jul 15 16:09:45 1985 Date-Received: Thu, 18-Jul-85 05:44:34 EDT References: <899@teddy.UUCP> <729@asgb.UUCP> Organization: Burroughs Corp. ASG, Boulder Colo. Lines: 33 > > 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. One would hope that the compiler (or optimizer) would realize what's going on here, and do something a little more clever than looping. Especially since this is a strncpy, so the check for the null character isn't done. I must admit that I am new to Unix C, but on VMS (and I beleive TOPS20, for whoever uses that) C, the compiler will turn this into a MOVC instruction (or a BLT on the '20). These are both the accepted method for filling a region. Mark Hamilton -- The opinions expessed above are -- or maybe they aren't... Mark Hamilton - Burroughs Advanced Systems Group Boulder, CO 80301 ihnp4!sdcsvax!bmcg!asgb!mah