Relay-Version: version B 2.10 5/3/83; site utzoo.UUCP Posting-Version: version B 2.10.1 6/24/83; site pucc-h Path: utzoo!watmath!clyde!burl!ulysses!mhuxr!mhuxn!ihnp4!inuxc!pur-ee!CS-Mordred!Pucc-H:ags From: ags@pucc-h (Dave Seaman) Newsgroups: net.lang.c Subject: Re: C programming hint Message-ID: <2128@pucc-h> Date: Sat, 13-Jul-85 14:27:18 EDT Article-I.D.: pucc-h.2128 Posted: Sat Jul 13 14:27:18 1985 Date-Received: Sun, 14-Jul-85 09:18:47 EDT References: <899@teddy.UUCP> <476@mtunh.UUCP> Reply-To: ags@pucc-h.UUCP (Dave Seaman) Organization: Purdue University Computing Center Lines: 44 Summary: In article <476@mtunh.UUCP> mgh@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