A follow up question to: Print 1 to 1000 with out using loop A question that was asked today and is a duplicate of an older question showing a code snippet of a recursion of the main function :
This code means to count to 10 without using any loops, it does it by recursing on the main functions pointer and when it reaches the needed amount of prints it changes the pointer to the exit function.
#include <stdio.h>
#include <stdlib.h>
void main(int j) {
printf("%d\n", j);
(&main + (&exit - &main)*(j/10))(j+1);
}
Note i changed the number of times the value will be printed, I do understand that the subtraction of function pointers is UB but still I compiles and ran this function with:
./mainexit 547
And for some reason the values that are beeing printed are from 2 to 10.
when i start the program with ./mainexit 1
it will print until j reaches 10 and then the reference will be for exit but when I run the program with 547 from the beginning the first function in the recursion that is being called located at address 57*&exit so it should do really weird stuff but instead it prints normally. any ideas on how the function returns to the main?