By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
432,002 Members | 2,137 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 432,002 IT Pros & Developers. It's quick & easy.

main(int argc, char *argv[])

P: n/a
I have my main function set up as

int main(int argv, char *argv[])

so taht i can read in a variable which is passed to the program on the
command line. The problem is that main calls other functions and some
of them need to start jump to the start of main. If my main setup was
just int main() this would be no problem, i could just call main();

The problem is main needs parameters passed to it now. Would any1
know what parameters I could pass to vall the main program??

argv[1] is just used once as soon as main starts up and never used
again after, so i dont mind if i loose the ability to read from it, i
just need to be able to call main from another part of the program

Thanks and regards
Nov 14 '05 #1
Share this Question
Share on Google+
13 Replies


P: n/a

Sokar wrote:
I have my main function set up as

int main(int argv, char *argv[])

so taht i can read in a variable which is passed to the program on the command line. The problem is that main calls other functions and some of them need to start jump to the start of main. If my main setup was just int main() this would be no problem, i could just call main();

The problem is main needs parameters passed to it now. Would any1
know what parameters I could pass to vall the main program??

argv[1] is just used once as soon as main starts up and never used
again after, so i dont mind if i loose the ability to read from it, i
just need to be able to call main from another part of the program

Thanks and regards


Recursively calling main is a bit funky. Why not have main call some
other
function (which takes no arguments) which is then used for the
recursion?

Anyway, you could always call main later with main(0,NULL).
Or if you want, you could squirrel away (in a static) the original
argc and argv and reinvoke main with them later.

-David

Nov 14 '05 #2

P: n/a
Sokar wrote:

I have my main function set up as

int main(int argv, char *argv[])

so taht i can read in a variable which is passed to the program on the
command line. The problem is that main calls other functions and some
of them need to start jump to the start of main. If my main setup was
just int main() this would be no problem, i could just call main();

The problem is main needs parameters passed to it now. Would any1
know what parameters I could pass to vall the main program??

argv[1] is just used once as soon as main starts up and never used
again after, so i dont mind if i loose the ability to read from it, i
just need to be able to call main from another part of the program


Don't call main(). Instead, have main() do what it needs to do with
argc/argv, then call the recursive function, and have everything call
that instead of main().

In fact, I believe I've seen in this group that calling main() is
specifically forbidden by the standard. (Or at least it's not
standard behavior, being either "implementation defined" or
"undefined behavior".)

--
+-------------------------+--------------------+-----------------------------+
| Kenneth J. Brody | www.hvcomputer.com | |
| kenbrody/at\spamcop.net | www.fptech.com | #include <std_disclaimer.h> |
+-------------------------+--------------------+-----------------------------+
Don't e-mail me at: <mailto:Th*************@gmail.com>

Nov 14 '05 #3

P: n/a

On Fri, 29 Apr 2005, Kenneth Brody wrote:

Don't call main(). Instead, have main() do what it needs to do with
argc/argv, then call the recursive function, and have everything call
that instead of main().
Good advice. I usually write a function called 'process',
int process(FILE *in, FILE *out);
and have it do all the work, 'main' being a wrapper around it. So if
for some reason I ever needed to perform the whole operation recursively,
it wouldn't be too hard.
Of course, the OP should think carefully about what he's trying to
do; recursing on any significant part of the program is usually the sign
of a newbie mistake, as with those beginners who write

int get_user_command_from_menu() {
int choice;
printf("Choose something (1-5).\n");
scanf("%d", choice); /* no error-checking! */
if (1 <= choice && choice <= 5)
return choice;
else {
printf("That was an invalid choice.\n");
printf("Please select 1, 2, 3, 4, or 5.\n");
return get_user_command_from_menu();
}
}

In fact, I believe I've seen in this group that calling main() is
specifically forbidden by the standard. (Or at least it's not
standard behavior, being either "implementation defined" or
"undefined behavior".)


Wrong. Calling 'main' recursively in C is perfectly fine and okay.
You may be thinking of The Language That C Is Not, in which calling
'main' /is/ disallowed.

HTH,
-Arthur
Nov 14 '05 #4

P: n/a
jm**********@hotmail.com (Sokar) writes:
I have my main function set up as

int main(int argv, char *argv[])

so taht i can read in a variable which is passed to the program on the
command line. The problem is that main calls other functions and some
of them need to start jump to the start of main. If my main setup was
just int main() this would be no problem, i could just call main();


As others have mentioned, if you really want recursion it's best to
have a separate recursive function that you call from main. It's
legal to call main recursively, but the new invocation will (probably)
have no way of knowing that it's not the original one, so it will try
to process the command-line arguments all over again. (You can pass
in extra information in the parameters, or you can use a static
variable, but both approaches are kludges.)

But since you said you need to "jump to the start of main", I suspect
what you really need is not recursion but simply a loop. Recursion is
a powerful tool, but you shouldn't use it unless it's appropriate.

--
Keith Thompson (The_Other_Keith) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.
Nov 14 '05 #5

P: n/a
David Resnick wrote:
Sokar wrote:
I have my main function set up as

int main(int argv, char *argv[])

so taht i can read in a variable which is passed to the program on


the
command line. The problem is that main calls other functions and


some
of them need to start jump to the start of main. If my main setup


was
just int main() this would be no problem, i could just call main();

The problem is main needs parameters passed to it now. Would any1
know what parameters I could pass to vall the main program??

argv[1] is just used once as soon as main starts up and never used
again after, so i dont mind if i loose the ability to read from it, i
just need to be able to call main from another part of the program

Thanks and regards

Recursively calling main is a bit funky. Why not have main call some
other
function (which takes no arguments) which is then used for the
recursion?

Anyway, you could always call main later with main(0,NULL).
Or if you want, you could squirrel away (in a static) the original
argc and argv and reinvoke main with them later.


Not exactly. argv[argc] must be NULL, i.e. you need
main(0, p), where *p==NULL.

Cheers
Michael
--
E-Mail: Mine is an /at/ gmx /dot/ de address.
Nov 14 '05 #6

P: n/a
Michael Mair wrote:
David Resnick wrote:
Recursively calling main is a bit funky. Why not have main call some other
function (which takes no arguments) which is then used for the
recursion?

Anyway, you could always call main later with main(0,NULL).
Or if you want, you could squirrel away (in a static) the original
argc and argv and reinvoke main with them later.


Not exactly. argv[argc] must be NULL, i.e. you need
main(0, p), where *p==NULL.

Cheers
Michael
--
E-Mail: Mine is an /at/ gmx /dot/ de address.


Um, why? I agree with what you say when main is originally invoked by
the implementation. If invoked recursively and one no longer cares
about using argc or argv why is main(0,NULL) not acceptable? In fact,
if one is recursively invoking main (probably a bad idea), having argv
be NULL seems
like one reasonable way to indicate this is not the original
invocation.

-David

Nov 14 '05 #7

P: n/a
jm**********@hotmail.com (Sokar) wrote:
I have my main function set up as

int main(int argv, char *argv[])

so taht i can read in a variable which is passed to the program on the
command line. The problem is that main calls other functions and some
of them need to start jump to the start of main. ^^^^
===SNIP===argv[1] is just used once as soon as main starts up and never used
again after, so i dont mind if i loose the ability to read from it, i
just need to be able to call main from another part of the program

^^^^

So which is it -- jump or call? If 'jump', there's setjmp/longjmp.

--
Dan Henry
Nov 14 '05 #8

P: n/a
David Resnick wrote:
Michael Mair wrote:
David Resnick wrote:
Recursively calling main is a bit funky. Why not have main call
some
other
function (which takes no arguments) which is then used for the
recursion?

Anyway, you could always call main later with main(0,NULL).
Or if you want, you could squirrel away (in a static) the original
argc and argv and reinvoke main with them later.


Not exactly. argv[argc] must be NULL, i.e. you need
main(0, p), where *p==NULL.


Um, why? I agree with what you say when main is originally invoked by
the implementation. If invoked recursively and one no longer cares
about using argc or argv why is main(0,NULL) not acceptable? In fact,
if one is recursively invoking main (probably a bad idea), having argv
be NULL seems like one reasonable way to indicate this is not the original
invocation.


Hmmm, probably just because I hate breaking calling conventions;
I only thought of that but not about the specific situation...
Apart from that, the only time when I had seen a slightly justified(*)
recursive call of main() was for something like
"if (argc) main(argc-1, argv+1);"
However, thinking about it once again, you are right -- most people
do not rely on, let alone use, argv[argc]==NULL, so we might abuse
the second argument to flag a recursive call (I still do not like
it, though).

Regards
Michael
____
(*) The small amount of justification came from a code length limit
and an otherwise clear structure... :-)
--
E-Mail: Mine is an /at/ gmx /dot/ de address.
Nov 14 '05 #9

P: n/a
Michael Mair wrote:
David Resnick wrote:

.... snip ...

Anyway, you could always call main later with main(0,NULL).
Or if you want, you could squirrel away (in a static) the
original argc and argv and reinvoke main with them later.


Not exactly. argv[argc] must be NULL, i.e. you need
main(0, p), where *p==NULL.


No, they will be so on the initial call of main. Once that happens
what you do next is up to you. For example, argc will hold a
positive value on the initial call. You could use that to detect a
recursive call by passing a negative argument. It all sounds
highly purposeless though.

Stoopid program, should run and terminate:

int main(int a, char **v)
{
if (a > 0) main(-5, 0); /* do 1st recursion */
else if (a < 0) ( /* in recursive call */
/* make it known if you wish */
main(a+1, 0);
}
return 0;
}

--
"If you want to post a followup via groups.google.com, don't use
the broken "Reply" link at the bottom of the article. Click on
"show options" at the top of the article, then click on the
"Reply" at the bottom of the article headers." - Keith Thompson
Nov 14 '05 #10

P: n/a
CBFalconer wrote:
Michael Mair wrote:
David Resnick wrote:


... snip ...
Anyway, you could always call main later with main(0,NULL).
Or if you want, you could squirrel away (in a static) the
original argc and argv and reinvoke main with them later.


Not exactly. argv[argc] must be NULL, i.e. you need
main(0, p), where *p==NULL.

No, they will be so on the initial call of main. Once that happens
what you do next is up to you. For example, argc will hold a
positive value on the initial call. You could use that to detect a
recursive call by passing a negative argument. It all sounds
highly purposeless though.


You are of course right, see my reply to David Resnick's answer.
Cheers
Michael
--
E-Mail: Mine is an /at/ gmx /dot/ de address.
Nov 14 '05 #11

P: n/a


Dan Henry wrote:
jm**********@hotmail.com (Sokar) wrote:

I have my main function set up as

int main(int argv, char *argv[])

so taht i can read in a variable which is passed to the program on the
command line. The problem is that main calls other functions and some
of them need to start jump to the start of main.


^^^^
===SNIP===
argv[1] is just used once as soon as main starts up and never used
again after, so i dont mind if i loose the ability to read from it, i
just need to be able to call main from another part of the program


^^^^

So which is it -- jump or call? If 'jump', there's setjmp/longjmp.


Calvin: Do we have any chainsaws in the house?
His Mother: No.
Calvin: Then how am I supposed to learn to juggle?

Sokar has asked how to do something that is almost
certainly better not done. Other points in his question
suggest he's not an experienced C juggler, certainly not
ready for the perils of longjmp(). Find out what he's
trying to do, not what he's asking: there's very likely
a better (and easier) way to get to his real goal.

But unless you like watching involuntary amputations,
please don't tell him where the chainsaws are.

--
Er*********@sun.com

Nov 14 '05 #12

P: n/a
Sokar wrote on 29/04/05 :
I have my main function set up as

int main(int argv, char *argv[])

so taht i can read in a variable which is passed to the program on the
command line. The problem is that main calls other functions and some
of them need to start jump to the start of main. If my main setup was
just int main() this would be no problem, i could just call main();

The problem is main needs parameters passed to it now. Would any1
know what parameters I could pass to vall the main program??

argv[1] is just used once as soon as main starts up and never used
again after, so i dont mind if i loose the ability to read from it, i
just need to be able to call main from another part of the program

Thanks and regards


Why in the world to you have to call main() ? While it is technically
possible in C (but not in C++, for good reasons, I guess), it exposes a
design error. There are enough code structure in C to avoid that.

- functions
- while
- do-while
- for

Write a better algorithm and stop writing spaghetti code, please...

--
Emmanuel
The C-FAQ: http://www.eskimo.com/~scs/C-faq/faq.html
The C-library: http://www.dinkumware.com/refxc.html

..sig under repair

Nov 14 '05 #13

P: n/a
(supersedes <mn***********************@YOURBRAnoos.fr>)

Sokar wrote on 29/04/05 :
I have my main function set up as

int main(int argv, char *argv[])

so taht i can read in a variable which is passed to the program on the
command line. The problem is that main calls other functions and some
of them need to start jump to the start of main. If my main setup was
just int main() this would be no problem, i could just call main();

The problem is main needs parameters passed to it now. Would any1
know what parameters I could pass to vall the main program??

argv[1] is just used once as soon as main starts up and never used
again after, so i dont mind if i loose the ability to read from it, i
just need to be able to call main from another part of the program

Thanks and regards


Why in the world do you have to call main() ? While it is technically
possible in C (but not in C++, for good reasons, I guess), it exposes a
design error. There are enough code structure in C to avoid that.

- functions
- while
- do-while
- for

Write a better algorithm and stop writing spaghetti code, please...

--
Emmanuel
The C-FAQ: http://www.eskimo.com/~scs/C-faq/faq.html
The C-library: http://www.dinkumware.com/refxc.html

"C is a sharp tool"

Nov 14 '05 #14

This discussion thread is closed

Replies have been disabled for this discussion.