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

    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.

Reply
  • "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.

Children