i have a question, how to pass arguments from other functions to main. i want to write a program like nano well not exactly like nano editor. I have a function f_read(char* filename[]), and fopen() get filename[1] as file name and *filename[2] as "r" read mode and rest of the code will read from a file.
I want is this char filename[] to main(int argc , char argv[])
I'm sure you can call main from other functions as long as main exists in the scope of the function:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
#include <cstdio>
void callback(int);
int main(int argc, char **argv) {
printf("Argument name is %s\n", *argv);
callback(argc);
return 0;
}
void callback(int p) {
char *cal[] = { "callback" };
p ^ 2 ? main(-~p, cal) : 0;
printf("%d\n", p);
}
But generally whenever you find yourself calling main, you have to rethink your approach to solving that problem. Calling main is never an option, the above example shows that it is possible but generally frowned upon.
Use a break statement if you are calling main to break out of a loop, use a return type if you want to get back to main from a function.
> But generally whenever you find yourself calling main,
> you have to rethink your approach to solving that problem.
Without exception, whenever you find yourself calling main() (which is at file scope), and the C++ program compiles cleanly, you do not have to think - just throw that particular compiler away.
The name main is not otherwise reserved; you could do something like this instead: