Relay-Version: version B 2.10 5/3/83; site utzoo.UUCP
Posting-Version: version B 2.10 5/3/83; site mtunh.UUCP
Path: utzoo!watmath!clyde!burl!ulysses!mhuxr!mhuxt!houxm!mtuxo!mtunh!mgh
From: mgh@mtunh.UUCP (Marcus Hand)
Newsgroups: net.lang.c
Subject: Re: C programming hint
Message-ID: <476@mtunh.UUCP>
Date: Thu, 11-Jul-85 16:47:12 EDT
Article-I.D.: mtunh.476
Posted: Thu Jul 11 16:47:12 1985
Date-Received: Sat, 13-Jul-85 10:46:11 EDT
References: <899@teddy.UUCP>
Organization: AT&T Information Systems Laboratories, Holmdel, NJ
Lines: 41

> Reply-To: kps@teddy.UUCP (Kesavan P. Srinivasan)
> Keywords: strncpy
> Summary: initializing strings (arrays of characters)
> 
> 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.

Sorry to dissapoint you, but
	1.	strncpy() is implemented using a loop (this side effect
		wouldn't work if it wasn't);

	2.	it only works if strncpy is implemented by starting with the
		first character to be moved and not the last (you can move
		up and down a string in either direction). I.e. it's
		implementation dependent;
	
	3.	its not recursive or even analogous to recursion -- it just
		keeps copying the last character it moved;

	4.	whats wrong with memset() (memory(3))  --  I don't know
		whether its any more efficient, but its certainly
		clearer to the reader what you are trying to achieve,
		and safer because its not implementation dependant:

			rtn = memset (buffer, ' ', buflen);
	
	Hope this helps you.
-- 
			Marcus Hand	(mtunh!mgh)