Relay-Version: version B 2.10 5/3/83; site utzoo.UUCP Posting-Version: version B 2.10.2 9/18/84; site dataio.UUCP Path: utzoo!linus!decvax!tektronix!uw-beaver!uw-june!entropy!dataio!bright From: bright@dataio.UUCP (Walter Bright) Newsgroups: net.lang.c Subject: Re: C programming hint Message-ID: <703@dataio.UUCP> Date: Fri, 12-Jul-85 05:41:25 EDT Article-I.D.: dataio.703 Posted: Fri Jul 12 05:41:25 1985 Date-Received: Wed, 17-Jul-85 01:27:27 EDT Reply-To: bright@dataio.UUCP (Walter Bright) Organization: Data I/O Corp., Redmond WA Lines: 16 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. > char blanks[SIZE]; /* declare array of SIZE elements */ > > blanks[0] = ' '; /* initialize 1st element */ > > strncpy(blanks + 1, blanks, SIZE - 1); /* initialize entire array */ > >The trick is to use strncpy in an almost recursive way. This will fail if strncpy() copies starting from the end of the source string, rather than from the beginning. You are depending on an undefined side effect of your implementation of strncpy(). Try using memset(): memset(blanks,' ',SIZE-1);