Eventually the topic of recursion comes up in programming. It means "in the body of function xyz(), there is a call to function xyz()". More simply, the function calls itself. There is one cardinal rule.
Inside the recursive function, there MUST BE some sort of "exit condition" |
/* pgm29 source */
#include <stdio.h>
#include <string.h>
void reverse(char *, char *);
int main(void) {
char *srcstr = "Joe loves Tina's smile!";
char *startp;
char *endp;
int slen;
startp = srcstr;
slen = strlen(srcstr);
endp = startp + (slen - 1); // output from program helps see this
printf("\nCharacter string (length %d):\n\n%s\n\n", slen, srcstr);
printf("\"startp\" contains memory address %p, points to '%c'\n", (void *) startp, *startp);
printf("\"endp\" contains memory address %p, points to '%c'\n\n", (void *) endp, *endp);
printf("Two examples of \"pointer math\":\n");
printf("Character found at \"startp + 3\": %c\n", *(startp + 3));
printf("Character found at \"startp + 10\": %c\n\n", *(startp + 10));
printf("Reversed string:\n\n");
reverse(startp, endp);
printf("%c\n", *startp); // after exiting the recursion, we need to print one more character
printf("\n\n");
return(0);
}
/* as long as "backwards-moving pointer 'endp' */
/* does not equal 'startp', the recursion keeps */
/* going, where the function CALLS ITSELF */
void reverse(char *startp, char *endp) {
if (startp != endp) {
printf("%c", *endp);
endp--;
reverse(startp, endp);
}
}