main(int argc, char *argv[])

S

Sokar

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
 
D

David Resnick

Sokar said:
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
 
K

Kenneth Brody

Sokar said:
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:[email protected]>
 
A

Arthur J. O'Dwyer

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
 
K

Keith Thompson

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.
 
M

Michael Mair

David said:
Sokar said:
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
 
D

David Resnick

Michael said:
David said:
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

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
 
D

Dan Henry

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.
 
M

Michael Mair

David said:
Michael said:
David said:
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... :)
 
C

CBFalconer

Michael said:
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;
}
 
M

Michael Mair

CBFalconer said:
Michael said:
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

Eric Sosman

Dan said:
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.
 
E

Emmanuel Delahaye

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
 
E

Emmanuel Delahaye

(supersedes <[email protected]>)

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"
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,766
Messages
2,569,569
Members
45,042
Latest member
icassiem

Latest Threads

Top