strtod( )

B

Bill Cunningham

Since I've been told that char *argv[] or char **argv[] must be the
second parameter to main's command line structure I have turned to strtod( )
but can't get it to work so far. This function is probably used alot but I
obviously am not using it right.

#include <stdio.h>

int main(int argc, char **argv) {
if (argc != 3) {

fprintf(stderr,"usage error");
}
strtod(argv[1],(char**);
strtod(argv[2],(char**);

printf("%f"argcv[1],"divided by ","%f",argv[2]," is
",argv[1]/argv[2]);
}

This is one of many tried of strtod( ) I've tried on my own.

Bill
 
I

Ian Collins

Bill said:
Since I've been told that char *argv[] or char **argv[] must be the
second parameter to main's command line structure I have turned to strtod( )
but can't get it to work so far. This function is probably used alot but I
obviously am not using it right.

#include <stdio.h>

int main(int argc, char **argv) {
if (argc != 3) {

fprintf(stderr,"usage error");
}
strtod(argv[1],(char**);

How could this compile?

strtod() returns a value, which you are discarding.

The simplest use of strtod() has NULL for the second parameter, for example:

double d = strtod( argv[1], NULL );

You should clear errno and check its value for zero after the call to
make sure the conversion was successful.
 
B

Bill Cunningham

How could this compile?

strtod() returns a value, which you are discarding.

The simplest use of strtod() has NULL for the second parameter, for
example:

double d = strtod( argv[1], NULL );

I read or thought I read the second argument was a pointer.
 
I

Ian Collins

Bill said:
How could this compile?

strtod() returns a value, which you are discarding.

The simplest use of strtod() has NULL for the second parameter, for
example:

double d = strtod( argv[1], NULL );

I read or thought I read the second argument was a pointer.
It is, read your documentation again to see what the second parameter is
used for.

Please trim stuff you are not commenting on.
 
B

Barry Schwarz

Since I've been told that char *argv[] or char **argv[] must be the

I doubt if anyone has suggested the second form. If they have, you
have just learned an important Usenet lesson: some advice is wrong.
More likely, however, is the lesson that in programming you need to
pay more attention to the details. The second form of this parameter
is char** argv. The difference is not irrelevant...
second parameter to main's command line structure I have turned to strtod( )
but can't get it to work so far. This function is probably used alot but I
obviously am not using it right.

#include <stdio.h>

int main(int argc, char **argv) {

But apparently you knew the correct form. So maybe your opening
sentence was sucker bait and you hooked me.
if (argc != 3) {

fprintf(stderr,"usage error");

You have been here for a while. How many times have you seen messages
discussing the potential problems caused by not including a \n at the
end of the string to be displayed?
}
strtod(argv[1],(char**);

You might want to spend some time learning to balance your
parentheses.

You might also want to practice indenting consistently. Code that
easier to read is easier to debug.

When you look up a function in your reference, it should also tell you
which headers you need to include to use that function. That
information is not superfluous.
strtod(argv[2],(char**);

printf("%f"argcv[1],"divided by ","%f",argv[2]," is
",argv[1]/argv[2]);

This is just mind boggling. Do you really think you can just ignore
the function prototypes? Do you really think that placing arguments
adjacent to each other with no intervening punctuation will magically
convert and concatenate values? Do you really think two conversion
specifications are adequate for printing at least three variable
values?

Where is the int that main is supposed to return?
This is one of many tried of strtod( ) I've tried on my own.

Forget strtod. Try to get main correct first.



Remove del for email
 
N

Nick Keighley

On 25 Mar, 01:47, (e-mail address removed) wrote:

please leave some context in:

the prototype of main is
int main (void)
or int main (int argc, char *argv[])

or type compatible versions such as
int main (int argc, char **argv)

the external environment (eg. the OS) passes
the address of the first element of an array of
strings. Strings in C are char*s. Hence the 2nd parameter
must be a pointer to an array of ptrs to char.
In other words a ptr-to-ptr-to-char (char**).
So I guess it's char *argv[] and can't be double *argv[] if doubles
are wanted then.

no you don'e get any choice you pass strings. The strings
could *represent* floating point values.

myprogram 6.02e23

argc = 2
argv = {"myprogram", "6.02e23", 0}

[the first entry in argv is actually implementation
dependent]
 
D

Default User

Barry said:
[blither]

Forget strtod. Try to get main correct first.


I recommend not wasting your time. He's supposedly been learning C
since 2004. Nothing you say will make the least impression. Most likely
he will just be moving on to some new problem he doesn't understand.

Until Cunningham agrees to learn C by defined rules, including working
one problem at a time through to success, and looking up functions in
manuals or online, people should just shut him out.

Yes, I'm an ol' meany-head because I have no sympathy for his supposed
learning disability. Well, if that prevents him from learning C at all,
which it seems to, then he should find some more productive use of his
time.




Brian
 
B

Bill Cunningham

[snip]
But apparently you knew the correct form. So maybe your opening
sentence was sucker bait and you hooked me.

The above statement you refer to is a misprint.

Bill
 
B

Bill Cunningham

I'm sorry I mean the line saying
int main(int argc, char ** argv[])

is a mistake. It's not a triple pointer. I have learned strtod ( ) though I
misunderstood how the code worked first. I learn a subject then move on to
learn something else. I have embarrassingly chopped up printf though. In a
way I should know better.

Bill
 
B

Bill Cunningham

Ian Collins said:
So I guess it's char *argv[] and can't be double *argv[] if doubles
are wanted then.
What can? What or who are you replying to?

Someone I can't remember who said the main prototype over than void was
(int argc, char * argv[]) or use no array and a double pointer since your
saying the same thing. So I guess that means I can't change the prototype to
read double * argv[] since that is what I want to begin with. To bypass the
strtod later in the function.

Bill
 
M

Morris Dovey

Bill said:
Ian Collins said:
So I guess it's char *argv[] and can't be double *argv[] if doubles
are wanted then.
What can? What or who are you replying to?

Someone I can't remember who said the main prototype over than void was
(int argc, char * argv[]) or use no array and a double pointer since your
saying the same thing. So I guess that means I can't change the prototype to
read double * argv[] since that is what I want to begin with. To bypass the
strtod later in the function.

That's correct - main gets its arguments as strings and it's up
to you to interpret/convert them.
 
D

David Thompson

printf("%f"argcv[1],"divided by ","%f",argv[2]," is
",argv[1]/argv[2]);

This is just mind boggling. Do you really think you can just ignore
the function prototypes?

If the juxtaposition worked (see next) this wouldn't violate the
prototype -- only the semantics for printf's varargs, which aren't in
the prototype. (Since *printf/scanf are standard, the compiler can
know about them and e.g. gcc does, but not from the prototype.)
Do you really think that placing arguments
adjacent to each other with no intervening punctuation will magically
convert and concatenate values?

It does in awk, whose surface syntax is enough like C (at least the C
when it was created, K&R1) that on a cloudy night, in a fog, with your
eyes closed said:
Do you really think two conversion
specifications are adequate for printing at least three variable
values?
If the (inconsistent and probably unintended, and misspelled)
juxtaposition had in fact done one conversion, you would have two
conversion specifiers and two remaining data items -- if there were
some way for printf distinguish which was intended to be which, which
there isn't. (Plus the fact, already discussed, that argv has to be
a string and can't be a float or double.)

- formerly david.thompson1 || achar(64) || worldnet.att.net
 

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

Similar Threads

Command Line Arguments 0
C pipe 1
root(s) 6
oscillator 51
pow type problem 6
average true range 21
Print with command-line arguments 0
indentation 26

Members online

No members online now.

Forum statistics

Threads
473,775
Messages
2,569,601
Members
45,182
Latest member
alexanderrm

Latest Threads

Top