C programming hint

Dave Seaman ags at pucc-h
Sun Jul 14 04:27:18 AEST 1985


In article <476 at mtunh.UUCP> mgh at mtunh.UUCP (Marcus Hand) writes
(Concerning "almost recursive" use of strncpy):

> Sorry to dissapoint you, but...
> 	
> 	3.	its not recursive or even analogous to recursion -- it just
> 		keeps copying the last character it moved;

The operation 

	blanks[0] = ' '
	blanks[i] = blanks[i-1]		for i = 1,2,...,SIZE-1

is recursive in the mathematical sense, just as the familiar definition 
of the factorial function,

	fact(0) = 1
	fact(n) = n * fact(n-1) for n > 0

is recursive.  The fact that it is possible to fill an array with blanks
or to compute the factorial function without resorting to functions or
procedures that call themselves does not mean the underlying definitions
are not recursive.

Both operations also have nonrecursive definitions:

	blanks[i] = ' '		for i = 1,2,...,SIZE-1

	fact(n) = GAMMA(n+1)	for n = 0,1,2, ...

-------------------------------------------------------------------------
>> 	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.
-- 
Dave Seaman			 ..!pur-ee!pucc-h:ags



More information about the Comp.lang.c mailing list