473,320 Members | 2,110 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

main(int argc, char *argv[])

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
13 7716

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

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


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
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
(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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

19
by: Steven T. Hatton | last post by:
The short sample program listed below has some features that I find to be bad style. In particular, they fail to communicate the connection between names used in this program and the location in...
14
by: Hal Styli | last post by:
Is this a style thing? int main(int argc, char *argv ) or int main(int argc, char **argv ) i.e. *argv or **argv Why choose the latter?
1
by: mudanoman | last post by:
Hello, I am new to c and need help with a current program I am working on. Currently, the program (code below) input is as follows: program apple.test.com 51112 What I need is...
7
by: =?Utf-8?B?Vmlu?= | last post by:
Hi, I have a question. I created a simple executable program using Visual C++ from Visual Studio 6.0 This program is called from a script that passes in one argument. Now, my question is: ...
7
by: william | last post by:
I wonder how 'main(int argc, char ** argv)' is implemented? How does it get the string literals separated by whitespace from the stdin stream? And is there any difference between 'char**' and...
3
by: Bill Cunningham | last post by:
I have been having a little trouble with this page. http://www.physics.drexel.edu/courses/Comp_Phys/General/C_basics/#command-line I wanted to create a command called div that takes two...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
0
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.