×
INTELLIGENT WORK FORUMS
FOR COMPUTER PROFESSIONALS

Log In

Come Join Us!

Are you a
Computer / IT professional?
Join Tek-Tips Forums!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!

*Tek-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.

Students Click Here

why sscanf does not work for reversed array address

why sscanf does not work for reversed array address

why sscanf does not work for reversed array address

(OP)
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
 

RE: why sscanf does not work for reversed array address

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

RE: why sscanf does not work for reversed array address

(OP)
yes, exactly.
I got it. Thanks a lot for your help.
/Kenny

Red Flag This Post

Please let us know here why this post is inappropriate. Reasons such as off-topic, duplicates, flames, illegal, vulgar, or students posting their homework.

Red Flag Submitted

Thank you for helping keep Tek-Tips Forums free from inappropriate posts.
The Tek-Tips staff will check this out and take appropriate action.

Reply To This Thread

Posting in the Tek-Tips forums is a member-only feature.

Click Here to join Tek-Tips and talk with other members! Already a Member? Login

Close Box

Join Tek-Tips® Today!

Join your peers on the Internet's largest technical computer professional community.
It's easy to join and it's free.

Here's Why Members Love Tek-Tips Forums:

Register now while it's still free!

Already a member? Close this window and log in.

Join Us             Close