Re: compilation problem



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


.



Relevant Pages

  • Hex editor display - can this be more pythonic?
    ... I'm building a hex line editor as a first real Python programming exercise. ... I had considered using the .translatemethod of strings, however this would require a larger translation table than my printable string. ... Where printing chars are shown in parenthesis, characters with Python escape sequences will be shown as their escapes in parens., while non-printing chars with no escapes will be shown with nothing in parens. ...
    (comp.lang.python)
  • Re: Need a sysRPL CRC16 routine.
    ... Thanks for your thoughts on the string> hex and vice versa. ... Sysrpl is not particularly intuitive on this point. ... effort he has put into the Debug4x programming environment. ...
    (comp.sys.hp48)
  • Entering strings as user input but interpreting as Python input (sort of)
    ... I would want the quoted part to be interpreted as if I entered it into Python itself as: ... In other words, if a quoted string occurs in the user input, I want only that part to be treated as a Python string. ... The point of this in the context of the hex editor is that the user should be able to enter hex bytes without qualifications like "0xXX" but rather as simply: "0A 1B 2C" etc. but also be able to input a string without having to type in hex ASCII codes. ...
    (comp.lang.python)
  • Re: Converting ascii to hex
    ... Why such meaningless and unreadable ... hex constants? ... Did you perhaps mean that "ciptmp is a CString ... For that matter, since you don't show how the string is read in, how do you know that the ...
    (microsoft.public.vc.mfc)
  • I dont mind bug: BinToHex implementation/description do not match !
    ... The BinToHex procedure occurding to Delphi's help would return a 'null' ... to terminate it with a #0 in the first place. ... Call BinToHex to convert the binary value in a buffer into a string that is ... Buffer is a buffer of bytes that contains the binary value. ...
    (alt.comp.lang.borland-delphi)