main (int argc,char *argv[])

B

Bill Cunningham

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 parameters a
numerator and denomenator that works as such:

div 3 15

and the return should be 5. The coding on this page is pretty poor IMO.
Atleast I'm having trouble reading it so that's nothing really new. Does a
for loop always have to be used in the command line situation?

Bill
 
W

Walter Roberson

I wanted to create a command called div that takes two parameters a
numerator and denomenator that works as such:
and the return should be 5. The coding on this page is pretty poor IMO.
Atleast I'm having trouble reading it so that's nothing really new.

The coding on that page follows fairly standard patterns that you get
to recognize after not so long.
Does a
for loop always have to be used in the command line situation?

I am going to guess that you are referring to the 'for' that is used
in the sample programs on that page to loop over the command line
arguments. If so, then the answer is "No, it is not always needed".
The code in the section you pointed to is dealing with
arguments that might be in any order, and has to have some kind of
loop in order to process all the arguments that are given by the user.
Your proposed command has fixed arguments and so does not need to loop.

Imagine that you expanded your utility so that it took a list of numbers
and operations. For example, imagine that a valid command was

div 15 / 3 * 9 * 11 / 13

and performed the obvious calculations (though whether as integer or
floating point or rational would have to be decided.) Then for a situation
like that where there were an unknown number of arguments, you would
need some form of loop to go over all of them.
 
J

John Bode

[snip]
Does a for loop always have to be used in the command line situation?


For what you're doing, no. You have a fixed number of parameters, and
they come in a fixed order. You *know* that argv[1] is the string
representation of your numerator and that argv[2] is the string
representation of your denominator, so you can refer to them
directly. For example:

#include <stdlib.h>
#include <stdio.h>

int main(int argc, char **argv)
{
if (argc < 3)
{
printf("Usage: div numerator denominator\n");
}
else
{
double numerator = strtod(argv[1], (char **) NULL);
double denominator = strtod(argv[2], (char **) NULL);

/* do stuff with numerator and denominator */
}

return 0;
}

Generally, you use the loop method shown in the tutorial if the number
or order of the parameters isn't fixed.
 
B

Bill Cunningham

[snipe]

For example:
#include <stdlib.h>
#include <stdio.h>

int main(int argc, char **argv)
{
if (argc < 3)
{
printf("Usage: div numerator denominator\n");
}
else
{
double numerator = strtod(argv[1], (char **) NULL);
double denominator = strtod(argv[2], (char **) NULL);
OK John right here. Instead of the char** cast could I just use as
main's parameter,

int argc, double* argv[]) ? Does it have to be a pointer to pointer to char
?

Bill
 
B

Bill Cunningham

John,

I am having some trouble with div ( ). Here's the code I have that
compiles.

#include <stdio.h>
#include <stdlib.h>

main() {
int x=15,y=3;
printf("%i",div(x,y));
}

That's fine and the right answer. But what about dividing 15 by 3.14 ?
Aren't you supposed to declare a variable of type div_t and assign something
to it? Div ( ) takes ints too so I suppose there will have to be casting.
This is supposesd to be a struct and .quot and .rem is used somehow.

Bill
 
J

Joe Wright

Bill said:
John,

I am having some trouble with div ( ). Here's the code I have that
compiles.

#include <stdio.h>
#include <stdlib.h>

main() {
int x=15,y=3;
printf("%i",div(x,y));
}

That's fine and the right answer. But what about dividing 15 by 3.14 ?
Aren't you supposed to declare a variable of type div_t and assign something
to it? Div ( ) takes ints too so I suppose there will have to be casting.
This is supposesd to be a struct and .quot and .rem is used somehow.

Bill
Hi Bill-
Consider that stdlib declares..

typedef struct {
int quot;
int rem;
} div_t;

#include <stdio.h>
#include <stdlib.h>

int main(void) {
int x = 15, y = 3;
div_t dv = div(x, y);
printf("%d %d\n", dv.quot, dv.rem);
return 0;
}
 
B

Bill Cunningham

Hi Bill-
Consider that stdlib declares..

typedef struct {
int quot;
int rem;
} div_t;

#include <stdio.h>
#include <stdlib.h>

int main(void) {
int x = 15, y = 3;
div_t dv = div(x, y);
printf("%d %d\n", dv.quot, dv.rem);
return 0;
}
Oh Thanks Joe. Here's what I was doing and it was obviously wrong.

div_t dv;
dv=div(15,3);
printf("%d%d",dv(15,3),dv.quot,dv.rem);

I see now.

Bill
 
J

John Bode

[snipe]

For example:
#include <stdlib.h>
#include <stdio.h>
int main(int argc, char **argv)
{
if (argc < 3)
{
printf("Usage: div numerator denominator\n");
}
else
{
double numerator = strtod(argv[1], (char **) NULL);
double denominator = strtod(argv[2], (char **) NULL);

OK John right here. Instead of the char** cast could I just use as
main's parameter,

int argc, double* argv[]) ? Does it have to be a pointer to pointer to char
?

Bill

Yes; all command line parameters are passed to main as an array of
char*.
 
J

Joe Wright

Bill said:
Oh Thanks Joe. Here's what I was doing and it was obviously wrong.

div_t dv;
dv=div(15,3);
printf("%d%d",dv(15,3),dv.quot,dv.rem);

I see now.

Bill
Glad to be of help. I know you have a Space bar because you used it once
in the above snippet. If you use it more often your code might be as
pretty as mine. :)
 
B

Bill Cunningham

Yes; all command line parameters are passed to main as an array of

So I could write like you did with strtod. Or maybe write a functionlike
this parameter

double func(char &a, char&b);

func would take a pointer to char like this wouldn't it?

Bill
 
B

Barry Schwarz

So I could write like you did with strtod. Or maybe write a functionlike
this parameter

double func(char &a, char&b);

func would take a pointer to char like this wouldn't it?
The & operator cannot appear in a declaration. To declare pointers
use the * syntax, as you did in several previous messages.


Remove del for email
 

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

No members online now.

Forum statistics

Threads
473,756
Messages
2,569,540
Members
45,024
Latest member
ARDU_PROgrammER

Latest Threads

Top