Re: compilation problem
- From: Lew Pitcher <lpitcher@xxxxxxxxxxxx>
- Date: Tue, 17 Mar 2009 17:57:05 -0400
On March 17, 2009 18:24, in comp.lang.c, Bill Cunningham
(nospam@xxxxxxxxxxxxx) wrote:
The compiler is giving me erros like multichar something or other. I
want this utility to print decimal for hex numbers entered. Fine I did
that. I also want it to take a 'd' switch while entering a decimal to
print a hex number. I don't know where in the char **argv to look so I
just used **argv. What's wrong here?
#include <stdio.h>
#include <string.h>
int main(int argc, char **argv)
{
if (argc != 3) {
puts("hex usage error");
exit(EXIT_FAILURE);
}
if (**argv == '-d') {
Bill, you /know/ that you can't compare strings this way.
Just think about what you are trying to do here. Let me paraphrase the logic
you appear to want:
IF the second argument passed is a string that matches "-d",
THEN
IF the third argument passed is a string specifying a decimal number
THEN
CONVERT the third argument from a numeric string into an integer
PRINT the integer as a hex value
TERMINATE with an EXIT_SUCCESS returncode
ELSE
PRINT a message saying that the 3rd argument was not a decimal number
TERMINATE with an EXIT_FAILURE returncode
END-IF
ELSE
IF the second argument passed is a string specifying a hex number
THEN
CONVERT the second argument from a hex string into an integer
PRINT the integer as a decimal value
TERMINATE with an EXIT_SUCCESS returncode
ELSE
PRINT a message saying that the 2nd argument was not a hex number
TERMINATE with an EXIT_FAILURE returncode
END-IF
END-IF
Now, remember,
- to compare /strings/, you use a string compare function, like strcmp()
- to convert a number string to an integer, you use a string conversion
function like strtol()
- the pointer to the first argument string is argv[0] or *(argv+0) or *argv
the pointer to the second argument string is argv[1] or *(argv+1)
the pointer to the third argument string is argv[2] or *(argv+2)
- the first character of the first argument string is *argv[0] or **(argv+0)
or **argv
So, your test for a "-d" flag here /really/ needs to be a string comparison,
using the pointer to the second argument and a pointer to the string "-d".
Now, code that test, and show us what you've got.
long y = strtol(**argv, NULL, 10);
printf("%x\n", y);
return 0;
}
long x = strtol(argv[1], NULL, 16);
printf("%i\n", x);
return 0;
}
HTH
--
Lew Pitcher
Master Codewright & JOAT-in-training | Registered Linux User #112576
http://pitcher.digitalfreehold.ca/ | GPG public key available by request
---------- Slackware - Because I know what I'm doing. ------
.
- Follow-Ups:
- Re: compilation problem
- From: Bill Cunningham
- Re: compilation problem
- References:
- compilation problem
- From: Bill Cunningham
- compilation problem
- Prev by Date: Re: "Portable" C compilers?
- Next by Date: Re: small integer powers -- use pow() or explicit multiplication?
- Previous by thread: Re: compilation problem
- Next by thread: Re: compilation problem
- Index(es):
Relevant Pages
|