C programming hint

kps at teddy.UUCP kps at teddy.UUCP
Fri Jul 12 22:37:12 AEST 1985


	char blanks[SIZE];
	blanks[0] = ' ';
	strncpy(blanks + 1, blanks, SIZE - 1);	/* initialize entire array */

Here's what people said about my method:

>From panda!talcott!harvard!stew
>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!

>From panda!genrad!decvax!harpo!whuxlm!whuxl!houxm!ihnp4!mhuxn!mhuxr!ulysses!allegra!alice!ark
>Don't.  There is no guarantee that strncpy copies one character at a time.

>From panda!talcott!harvard!seismo!umcp-cs!chris
>Beware, that won't work on Vax systems that use movc3 for string copies
>(probably Sys VR2V2.2.2.2... [sorry :-)] and 4.3 both).

>From panda!genrad!decvax!harpo!whuxlm!whuxl!houxm!mtuxo!mtunh!mtung!mtunf!ariel!vax135!petsd!gangal
>2)      I don't know exactly what the standard C library does, but many
>	string copy routines (in micro-code or higher level) check
>	for overlapping strings and would process your request in reverse
>	to avoid just the recursion you want.  (For instance, you may
>	have a string in there to which you want to prepend a character.)

>From panda!genrad!decvax!harpo!whuxlm!whuxl!houxm!mtuxo!mtunh!mgh
>	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;

Well, as you can probably see, I've been bludgeoned to death
with the fact that my method is implementation dependent and
that it won't work all the time.

I stand connected, er, I mean corrected.

Thanks for a humbling experience,
			B.K.



More information about the Comp.lang.c mailing list