Re: sscanf problem.

Tech-Archive recommends: Fix windows errors by optimizing your registry



Control Freq schrieb:

char cs[3];
BYTE b;
m_EntryMac[0].GetWindowText(cs, 2);
sscanf(cs, "%X", &b);


This code causes a fault at the sscanf line, but only in release mode.
Works fine in debug mode.

Ov course the code fails. Read the documentation of sscanf, in particular the section about the "scanf Type Field Characters":

character: X
Type of input expcted: Hexadecimal integer
Type of argument: Pointer to *int*

You are giving sscanf a pointer to a BYTE, not a pointer to an int. But sscanf will write an int into the variable. Maybe it does not fail in debug mode because the memory on the stack aroud the B varriable is not used. You were just lucky.

Do something like this:

> BYTE b;
int i;
> sscanf(cs, "%X", &i);
b = (BYTE)i;

Norbert
.



Relevant Pages

  • Re: Debugging standard C library routines
    ... I wouldn't go so far as to advise to initialise _every_ pointer. ... In Debug Mode, maybe. ... int main ...
    (comp.lang.c)
  • [UNIX] PHP Local Buffer Underflow
    ... The following security advisory is sent to the securiteam mailing list, and can be found at the SecuriTeam web site: http://www.securiteam.com ... PHP Local Buffer Underflow ... pointer to pointer to zval structure past the end of argument array by ... so this way we pass our own pointers to sscanf. ...
    (Securiteam)
  • Re: how to count rows and columns of integers/doubles in a file?
    ... then use sscanf() to scan the resulting line. ... void getdata(char *filename, unsigned *pRows, unsigned *pCols); ... int countColsInNextLine; ... unsigned rows, cols, oldcols; ...
    (comp.lang.c)
  • Re: Strtol vs sscanf
    ... sscanf() format string. ... every pointer requires a dynamic allocation! ... > the malloc error's before exiting which variables failed to get ...
    (comp.lang.c)
  • Re: text reading
    ... int values_read; ... Read the documentation for sscanf. ... Fix any errors you see. ... Keith Thompson kst-u@xxxxxxx ...
    (comp.lang.c)