Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

why sscanf does not work for reversed array address 1

Status
Not open for further replies.

tjroamer

Programmer
Mar 25, 2005
8
DE
Hi gurus,
a behavior of sscanf is confusing me:
I wrote a program like this:
Code:
char *mac = "00:12:34:5a:6b:8f";
unsigned char da[6];
sscanf(mac, "%2x:%2x:%2x:%2x:%2x:%2x", da+5, da+4, da+3, da+2, da+1, da);
I found only the first byte of da is scaned correctly, but if I wrote the program like this, it just works fine:

Code:
sscanf(mac, "%2x:%2x:%2x:%2x:%2x:%2x", da, da+1, da+2, da+3, da+4, da+5);

since the sscanf will put the scaned value to the corresponding address, why did I get this confusing behavior?

Thanks for your tips in advance.
regards
Kenny
 
The %x format specification describes (pointer to) int (or unsigned int) variable in arg list of (s)scanf() - not to pointer to char arg as in the snippet above. Read these six values into int vars then assign these values to da char array elements.
That's all...
 
yes, exactly.
I got it. Thanks a lot for your help.
/Kenny
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top