This discussion has been locked.
You can no longer post new replies to this discussion. If you have a question you can start a new discussion

sscanf() does not work correctly

When I use following statement in the code which runs on ARM9, it does not correctly parse the IP address.
I am using standard function for sscanf().

main()
{ unsigned char ip[16];
sscanf("255.1.1.1","%u.%u.%u.%u",&ip[0],&ip[1],&ip[2],&ip[3]);
}

thanks in advance,
-pjs

Parents
  • I want to convert the charecters between the dots to the their integer values, hence using the %u.
    Main purpose of my program is to convert the charecter based ip address string to integer array.
    When I try to run same program pc with intel processor, it works fine.

Reply
  • I want to convert the charecters between the dots to the their integer values, hence using the %u.
    Main purpose of my program is to convert the charecter based ip address string to integer array.
    When I try to run same program pc with intel processor, it works fine.

Children
  • "When I try to run same program pc with intel processor, it works fine."

    Quite by accident because the PC is little-endian and tolerates unaligned integer accesses. scanf()-ing an unsigned integer into ip[0] overwrites ip[1] and quite likely ip[2] and ip[3] with zero. Try it on a big-endian architecture or one requires aligned accesses, but wear a bib when you do.

  • Many things are wrong with the code

    Assume the ip is aligned on a 32-bit boundry and littel endian

    The first %u puts

    255 into ip 0
    0 into ip 1
    0 into ip 2
    0 into ip 3

    The second %u tries to put startig at ip 1's address, but sing it is not 32-bit aligned, it "rounds down"

    The second %u puts
    1 into ip 0
    0 into ip 1
    0 into ip 2
    0 into ip 3

    The 3rd (this is also rounded down)

    puts 1 into ip 0
    0 into ip 1
    0 into ip 2
    0 into ip 3

    4th

    puts 1 into ip 0
    0 into ip 1
    0 into ip 2
    0 into ip 3

    which is what happens.

    This is what I would expect to happen

  • The only reason it runs on the PC is because it allows 32 bit access that are not 32-bit aligned.

  • "I want to convert the charecters between the dots to the their integer values, hence using the %u."

    You are confusing the general mathematical term "integer" - meaning a whole number (no fractional part) - with the specific 'C' data type 'int'

    %u tells sprintf to return a number using the int data type, and you supply an address to tell it where to store that int.

    Now, an int will be larger than a char; so, when sprintf stores its result it will start at the address you specify, and carry on from there with as many bytes as the compiler happens to use for an int - thus it will overwrite the next few elements in your char array.

  • Thanks a lot for the detailed answers !
    I have handcoded small function then to parse the string into four ascii words and converted it to corresponding integer values, which works correctly.
    Thanks again for your time.