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);
}
Related
#include <stdio.h>
#include <string.h>
struct students{
char name[50];
int age;
int height;
};
int main(int argc, char **argv)
{
struct students manoj;
strcpy(manoj.name, "manojkumar");
manoj.age = 15;
displaymanoj(&manoj); //print testing \n , name , age
return 0;
}
void displaymanoj(struct students *ptr) {
printf("Testing...............DEBUG\n");
printf("%s\t%d\n", ptr->name,ptr->age);
printf("END OF TEST: SUCESS -manoj-");
}
I am learning C and it's working where is use pointer to point to structure variable. I am getting the correct output when I run the program. Just that my Geany IDE giving out some message which I would like to know why.
My Compiler Message as below :
You must declare the functions before calling them.
So your program should look something like
// Includes
// Structure
// Function prototype declaration
// This was what you were missing before
void displaymanoj(struct students *ptr);
int main(int argc, char **argv)
{
...
}
void displaymanoj(struct students *ptr) {
...
}
Since you have the definition of displaymanoj() isn't seen when you call it from main(), compiler implicitly declares one with return type int
which conflicts with the actual one. Note that the implicit declaration has been removed since the C99 standard and is no longer valid.
To fix it:
1) Either move the function displaymanoj() above main()'s definition or
2) Do a forward declaration of displaymanoj().
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);
}
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 happen to have several functions which access different arguments of the program through the argv[] array. Right now, those functions are nested inside the main() function because of a language extension the compiler provides to allow such structures.
I would like to get rid of the nested functions so that interoperability is possible without depending on a language extension.
First of all I thought of an array pointer which I would point to argv[] once the program starts, this variable would be outside of the main() function and declared before the functions so that it could be used by them.
So I declared such a pointer as follows:
char *(*name)[];
Which should be a pointer to an array of pointers to characters. However, when I try to point it to argv[] I get a warning on an assignment from an incompatible pointer type:
name = &argv;
What could be the problem? Do you think of another way to access the argv[] array from outside the main() function?
char ** name;
...
name = argv;
will do the trick :)
you see char *(*name) [] is a pointer to array of pointers to char. Whereas your function argument argv has type pointer to pointer to char, and therefore &argv has type pointer to pointer to pointer to char. Why? Because when you declare a function to take an array it is the same for the compiler as a function taking a pointer. That is,
void f(char* a[]);
void f(char** a);
void f(char* a[4]);
are absolutely identical equivalent declarations. Not that an array is a pointer, but as a function argument it is
HTH
This should work,
char **global_argv;
int f(){
printf("%s\n", global_argv[0]);
}
int main(int argc, char *argv[]){
global_argv = argv;
f();
}
#include <stdio.h>
int foo(int pArgc, char **pArgv);
int foo(int pArgc, char **pArgv) {
int argIdx;
/* do stuff with pArgv[] elements, e.g. */
for (argIdx = 0; argIdx < pArgc; argIdx++)
fprintf(stderr, "%s\n", pArgv[argIdx]);
return 0;
}
int main(int argc, char **argv) {
foo(argc, argv);
}
I was writing a C program where I use 6 variables a,b,c,d,e,f
a,b,c are constant values which I should pass as an arguments from the command line.
d,e,f are going to be size of arrays of a structure.
typedef struct
{
blah blah
} ex;
ex ex0[d];
I am very confused about how to pass all these as argument. Right now I have hard coded these values,which apparently I should not be doing.
This should get you started:
int main(int argc, char* argv[]) {
// argc - number of command line arguments
// argv - the comand line arguments as an array
return 0;
}
All params you pass to the program are stored in second argument of main function
int main(int argc, char* argv[]) // or int main(argc, char** argv)
so you can easily access 4th parameter by argc[3]. But it is not int, it is string, so you need to parse it. There are standard libraries for both taking you actual parameters from argc and parsing them for type you need. But in casual progrmas there is no point using them, so your code may look like this:
typedef struct
{
blah blah
} ex;
int main(int argc, char* argv[])
{
ex ex0[(int)argv[3]]; // i am not sure if it works on pure C, so you can try int atoi(char *nptr) from stdlib.h
}
Use command line arguments
int main(int argc, char* argv[]) // or int main(int argc, char **argv)
{
// argc is the argument count
//argv : The array of character pointers is the listing of all the arguments.
//argv[0] is the name of the program.
//argv[argc] is a null pointer
}