Can somebody please explain this recursive function for me?

Posted by capncoolio on Stack Overflow See other posts from Stack Overflow or by capncoolio
Published on 2012-06-10T09:59:39Z Indexed on 2012/06/10 10:40 UTC
Read the original article Hit count: 154

Filed under:
|
|
|
|
#include <stdio.h>
#include <stdlib.h>

void reprint(char *a[]) {
    if(*a) {
            printf("%d ",a);
            reprint(a+1);
            printf("%s ",*a);
    }
}

int main() {
    char *coll[] = {"C", "Objective", "like", "don't", "I", NULL};
    reprint(coll);
    printf("\n");
    return EXIT_SUCCESS;
}

As the more experienced will know, this prints the array in reverse. I don't quite understand how!

I need help understanding what reprint(char *a[]) does. I understand pointer arithmetic to a degree, but from inserting printf's here and there, I've determined that the function increments up to the array end, and then back down to the start, only printing on the way down. However, I do not understand how it does this; all I've managed to understand by looking at the actual code is that if *a isn't NULL, then call reprint again, at the next index.

Thanks guys!

© Stack Overflow or respective owner

Related posts about c

    Related posts about arrays