Problem with sscanf

Halo, I am facing a problem with sscanf function which my program will crash when executing the sscanf function.

Following are the code:

uint8* InputTime;

uint16 Hours;
uint16 Minutes;
uint16 Seconds;
sscanf((const char*)InputTime,"%d:%d:%d", &Hours, &Minutes, &Seconds);


The InputTime is pointing to a string which have following format "number(in ASCII):number(in ASCII):number(in ASCII)"

example: "12:43:14"

However, my program will crash when executing the sscanf function.

Can anyone tell me what have i done wrong? Please advice! Thanks!
sscanf assumes the variables you pass are at least of size int (either that, or of size long -- but I'd wager it's more likely int).

Because uint16 is smaller than an int (2 bytes instead of most likely 4), sscanf is writing outside of your buffer which causes memory corruption, which is why your program is crashing.

To fix this, make all your variables ints instead of uint16s. If the variables must be uint16s, call sscanf with temporary ints, and then copy those ints to your real variables.
Topic archived. No new replies allowed.