Relay-Version: version B 2.10 5/3/83; site utzoo.UUCP
Posting-Version: version B 2.10.1 6/24/83 (MC840302); site enea.UUCP
Path: utzoo!linus!decvax!genrad!panda!talcott!harvard!seismo!mcvax!enea!bo
From: bo@enea.UUCP (Bo Steinholtz)
Newsgroups: net.lang.c
Subject: Re: C programming hint
Message-ID: <882@enea.UUCP>
Date: Fri, 12-Jul-85 16:09:05 EDT
Article-I.D.: enea.882
Posted: Fri Jul 12 16:09:05 1985
Date-Received: Mon, 15-Jul-85 02:20:22 EDT
References: <899@teddy.UUCP>
Reply-To: bo@enea.UUCP (Bo Steinholtz)
Organization: ENEA DATA, Sweden
Lines: 28
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 

Ah, how the heart of an old assembler language programmer rejoices!
For another description of this trick,
see e.g. the description of the MVC machine instruction in
"IBM System/360 Principles of Operation" from the sixties,
where it is pointed out that it may be employed to
initialize an array in one machine instruction.

However, the most authoritative (?) source on C,
Harbison/Steele: "C: A Reference Manual", Prentice-Hall 1984,
states in the definition of strncpy that:

	"The results are unpredictable if the two string
	arguments overlap in memory."

Thus, if strncpy is used, the trick is nonportable!