简体   繁体   中英

Pointer to main function in c

When we make a pointer to the main function and call it is it duplicating the data in the stack memory like in recursive or just point at it and start over.

This is valid C code:

int main(void) {
    main();
}

If you compile and run it, you will notice it will crash with a segmentation fault. You're basically blowing up the stack. This shows it is actually consuming a bit of the stack on each nested call.


As a further example:

Like any recursive function, you need to have a mechanism that guarantees a recursion limit within the range of what your environment allows you to use.

#include <stdio.h>

int main(int argc, char *argv[]) {
    if (argc > 1) {
        main(--argc, ++argv);
        printf("%s\n", *argv);
    }
}

This will print all command line arguments in reverse order.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM