Relay-Version: version B 2.10 5/3/83; site utzoo.UUCP Posting-Version: version B 2.10.2 9/18/84; site harvard.ARPA Path: utzoo!watmath!clyde!burl!ulysses!allegra!mit-eddie!think!harvard!stew From: stew@harvard.ARPA (Stew Rubenstein) Newsgroups: net.lang.c Subject: Re: C programming hint Message-ID: <244@harvard.ARPA> Date: Wed, 10-Jul-85 23:06:19 EDT Article-I.D.: harvard.244 Posted: Wed Jul 10 23:06:19 1985 Date-Received: Fri, 12-Jul-85 04:43:33 EDT References: <899@teddy.UUCP> Reply-To: stew@harvard.UUCP (Stew Rubenstein) Organization: Aiken Computation Laboratory, Harvard Lines: 22 Keywords: strncpy In article <899@teddy.UUCP> kps@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