C programming hint

Tim Smith tim at callan.UUCP
Tue Jul 30 11:11:27 AEST 1985


> 	char blanks[SIZE];	/* declare array of SIZE elements */
> 	
> 	blanks[0] = ' ';	/* initialize 1st element */
> 
> 	strncpy(blanks + 1, blanks, SIZE - 1);	/* initialize entire array */

Danger Will Robinson! Danger Will Robinson!  Non-portable contruct.  Unsafe
assumptions about implementation of strncpy.  Danger Will Robinson!

If strncpy is done in assembly for efficiency, and the machine has a block
tranfer instruction, and the author of strncpy used it, and the machine is
smart about copying strings, then the above will fail, because the machine
will recognize that the source and destinations overlap, with the destination
above the source, so it will copy the string from the back.
-- 
					Tim Smith
				ihnp4!{cithep,wlbr!callan}!tim



More information about the Comp.lang.c mailing list