I was going through the difference in C and C++ and I found a tricky point. Can you please elaborate the below points:
In C, we can call main() Function through other Functions.
In C++, we cannot call main() Function through other functions.
How to call main() from another function and what is the use case of it?
#TrevorHickey hit the nail on the head (where did his answer go?) - C++ forbids calling main from within a different function (for good reason)... Not that any compiler is likely to stop you (I don't think most of them care).
An obvious workaround would be to move main's functionality into a container function and call it from there, as suggested by #KlasLindbäck.
i.e.
int my_application(int argc, char const * argv[]) {
// do stuff
return 0;
}
int main(int argc, char const * argv[]) {
return my_application(argc, argv);
}
Another "hack" that probably only works because compilers let you call main anyway (As also pointed out by #KlasLindbäck in the comments), would be to use function pointers. i.e.
int main(int argc, char const * argv[]) {
// do stuff
}
// shouldn't compile... but hey, you never know.
int (*prt_to_main)(int, char const* argv[]) = main;
void test_run(void) {
prt_to_main(0, NULL);
}
Related
I am trying to create a before-main evaluation like this:
int evaluate(int argc, char** argv) __attribute__ ((constructor));
int evaluate(int argc, char** argv)
{
int result = atoi(argv[1]);
if (result == 0)
return 1;
else
return 0;
}
int main(?????)
{
if (????? == 0)
printf("Wrong number.");
else
printf("It is 1!");
}
Is there a way to do this? I am doing this purely to get myself more familiar with C, passing command line arguments and using pre-main functions.
This is very platform-dependent. The glibc dynamic loader passes the argc, argv, and envp to ELF constructors, so you can access the program arguments in this way. To my knowledge, this is an undocumented dynamic loader feature, so you probably should not rely on this behavior.
For the result of the check, you will have to write it to a global variable (or record it in some other global data structure). The implementation discards the
In glibc, main is documented as either,
int main (int argc, char *argv[])
Or,
int main (int argc, char *argv[], char *envp[])
Can you define all the arguments as being const if you don't want to change them?
int main (const int argc, const char * const argv[])
Is it supported, unsupported, or illegal?
In C, the implementation is allowed to support basically any type for the main function, so it may very well be that your particular implementation allows the various forms you have proposed. (And indeed it seems to allow the three-parameter version that exposes the environment.) However, the implementation is only required to accept the two forms
int main(void)
and
int main(int, char**)
Since int(int, const char**) isn't the same type as int(int, char**), your proposed "constified" version is not strictly speaking required to be supported and falls under the first rule. It is, however, quite likely to work since char* and const char* are laid out the same way as far as the ABI is concerned, and the data you're given is mutable anyway.
Note further that int f(int) and int f(const int) are the same identical prototype, so there is no problem here regarding top-level qualifications of the parameters.
I'm a beginner with the langage C and i'd like to know what's the difference between this:
int main(int argc, const char * argv[])
and this:
int main(int argc, char * argv[])
I think it's the same thing but i'm not sure. Could someone explain me the difference.
Thank you
int main(int argc, char * argv[]) is correct and must be accepted by the compiler.
int main(int argc, const char * argv[]) may be rejected by the compiler. If the compiler accepts it then the behaviour is implementation-defined. This means that the compiler must document which non-standard signatures of main it accepts, and document the behaviour of each one.
So, consult your compiler's documentation to see what it says about parameters of main. A reasonable expectation would be that the compiler makes this form behave the same as if you had int main(int argc, char *__argv[]) { const char **argv = (const char **)__argv; .
I guess firstly you have to know const char* c; and char *const c;
In const char* c, const means you can't change the content c points to.
In char *const c, const means you can't change the position c points to.
As you know, the value stored in char* c is an address in memory, so the first const indicates that the value that c points to isn't supposed to be changed.And the second one indicates that the value of c shouldn't be changed.
Thus, in const char * argv[], argv is an array of const char* variables.This means the content that every element in argv points to is constant.Actually, each element points to a string, and argv is an array of strings storing "arguments" that CL passes to you.It's true that you can only read the arguments but modifying them.
Of course, compiler won't block you if const is missed here.If so, you can change the content of strings in your code and I guess nothing will happen, since these arguments are for you to read. :)
we declare main() as
int main(int argc, char *argv[])
and pass some argument by command line and use it
as argv[1], argv[2] in main() function definition but what if i want to use that in some other function's definition ?
one things i can do it always pass that pointer char** argv from main() to that function by argument. But is there any other way to do so?
Just pass the pointer?
int main(int argc, char** argv) {
do_something_with_params(argv);
}
void do_something_with_params(char** argv) {
// do something
}
Or if you mean passing single arguments:
int main(int argc, char** argv) {
do_something_with_argv1(argv[1]);
do_something_with_argv2(argv[2]);
}
void do_something_with_argv1(char* arg) {
// do something
}
void do_something_with_argv2(char* arg) {
// do something
}
In order to make data available to other functions, you need to pass it as a parameter, or make it available through a global (not recommended) or a static variable.
static char** args cmd_args;
static int cmd_arg_count;
int main(int argc, char** argv) {
cmd_arg_count = argc;
cmd_args = argv;
do_work();
return 0;
}
void do_work() {
if (cmd_args > 1) {
printf("'%s'\n", cmd_args[1]);
}
}
The best approach is to make a function that parses command line parameters, and stores the results in a struct that you define specifically for the purpose of representing command line arguments. You could then pass that structure around, or make it available statically or globally (again, using globals is almost universally a bad idea).
one things i can do it always pass this value from main() to that function by argument. But is there any other way to do so?
No. Passing the argc and argv to other functions is perfectly valid.
int main(int argc, char *argv[])
{
typedef struct _cmdline_arg_struct {
// all your command line arguments go here
}cmdline_arg_struct;
/* command line arguments - parsed */
cmdline_arg_struct *pc = (cmdline_arg_struct *) malloc(sizeof(cmdline_arg_struct));
if (parse_cmdline_args(&pc, argc, argv) == PARSE_FAILURE) {
usage();
return 0;
}
/* Now go through the structure and do what ever you wanted to do.. *?
}
You could have code in main() to store the values in non-local variables, and then refer to those variables from other functions.
If you really need to, store them in globals.
but what if i want to use that in some other function's definition?
You mean have another set of parameters for main? That's not possible, because main is defined in the C standard as either
int main();
or
int main(int, char*[]);
And it makes sense: The first parameter tells you the number of parameters given on the command line, the second contains the array of pointers to those parameters.
It's your responsibility to make sense of those parameters and pass them to other functions. Use string conversion functions if you need to make them numbers.
I was wondering how I could call a function in an other function in C.
For example, I created the function :
void speed (int argc, char** argv)
from a hyperterminal, I type 1 or 2 and I get something written on the hyperterminal.
In another function called:
void menu (int argc, char ** argv)
I want to call the previous function: speed(...). I dont know what to fill in for ....
thanks
jim
void menu (int argc, char ** argv)
{
speed(argc, argv); // this is how you call a function
}
For this to work either speed needs to be defined above menu, or a declaration of it needs to be before it or in a header. Declaration of speed looks like
void speed (int argc, char ** argv);
What Franco was talking about are called function prototypes. C parses (or compiles) your code from top down, so if it hits your call to speed before it hits your declaration of speed it complains. To workaround this, you need to create a function prototype as a forward reference to the function. Generally, it is good practice to write prototypes for all your functions at the top of your code, or in another file (aka the header file) and #include it.
/* helloworld.h */
void speed (int , char **);
void menu (int , char **);
/* helloworld.c */
#include "helloworld.h"
void menu (int argc, char **argv){
speed (argc, argc);
}