The problem with atoi and atof the way you suggested is that they don't adjust to varying length, then number of decimal digits cannot change because you placed the beginning of each argument at fixed positions. Isaac Em 14/08/2013 10:51, Tamas Rudnai escreveu: > Why do not you try atoi and atof instead of scanf? > > // 99 init values just for debugging resons > char string[] =3D "test1 1.525,2.789,12,13,14"; > char theChar1 =3D 99; > char theChar2 =3D 99; > char theChar3 =3D 99; > float theFloat1; > float theFloat2; > int retVal =3D 99; > > theFloat1 =3D atof(&string[6]); > theFloat2 =3D atof(&string[12]); > theChar1 =3D atof(&string[18]); > theChar2 =3D atof(&string[21]); > theChar3 =3D atof(&string[24]); > > Or better to use strtof and strtol as then you always know where to > continue to scan the input, so it will work even if Float1 has 4 fraction > digits or zero: > > // 99 init values just for debugging resons > char string[] =3D "test1 1.525,2.789,12,13,14"; > char theChar1 =3D 99; > char theChar2 =3D 99; > char theChar3 =3D 99; > float theFloat1; > float theFloat2; > int retVal =3D 99; > > char* input; > > theFloat1 =3D strtof(string+6, &input); > theFloat2 =3D strtof(input+1, &input); > theChar1 =3D strtol(input+1, &input, 10); > theChar2 =3D strtol(input+1, &input, 10); > theChar3 =3D strtol(input+1, &input, 10); > > Tamas > > > > > > > > > On 13 August 2013 22:19, Mascalzone Latino wrote: > >> Tamas, >> thanks a lot for your reply. Actaually I think that assigning to retVal = (an >> int, not a int *) following the sscanf prototype is correct. About your >> second (right) observation, I actually made (another) copy and paste >> mistake >> in the previous code example of my post. I added two additional %hhd in = the >> first sscanf instruction. Following the correct code, which is the one I >> really tested before writing my post. It doesn't work, the first sscanf = is >> correctly executed and theChar1 get the value 12, but the second sscanf >> crashes. >> >> *// 99 init values just for debugging resons >> char string[32] =3D "test1 1.525,2.789,12,13,14"; >> char theChar1 =3D 99; >> char theChar2 =3D 99; >> char theChar3 =3D 99; >> float theFloat1; >> float theFloat2; >> int retVal =3D 99; >> >> retVal =3D sscanf(&string[6],"%f,%f,%hhd",&theFloat1, &theFloat2, &theCh= ar1); >> // ALL OK! >> retVal =3D sscanf(&string[6],"%hhd", &theChar2); // IT CRASHES >> retVal =3D sscanf(&string[21],"%hhd", &theChar3); // NOT EXECUTED* >> >> And, if I don't use the last two instructions, it fails also if I replac= e >> them withe the following two: >> >> *retVal =3D sscanf("13","%hhd", &theChar2); // IT CRASHES >> retVal =3D sscanf("14","%hhd", &theChar3); // NOT EXECUTED* >> >> In order to speed up my project because, unfortunately, I have to delive= r >> this week, I went around the issue and found a solution that is not >> super-elegant but it works. It is: >> >> *char string[32] =3D "test1 1.525,2.789,12,13,14"; >> char theChar1 =3D 99; >> char theChar2 =3D 99; >> char theChar3 =3D 99; >> int tempInt1 =3D 99; >> int tempInt2 =3D 99; >> int tempInt3 =3D 99; >> float theFloat1; >> float theFloat2; >> int retVal =3D 99; >> >> retVal =3D sscanf(&string[6],"%f,%f,%d,%d,%d",&theFloat1, &theFloat2, &i= nt >> tempInt1, &int tempInt2, &int tempInt3); // ALL OK HERE! >> >> theChar1 =3D (char) tempInt1; >> theChar2 =3D (char) tempInt2; >> theChar3 =3D (char) tempInt3;* >> >> In my opinion it is not a stack issue. I say that without having deeply >> tested it, but I ran a test with a string much shorter of char string[32= ] =3D >> "test1 1.525,2.789,12,13,14"; above but still trying to get three char w= ith >> %hhd. It is probably due to %hhd format that the function crashes. But >> unfortunately, in the Microchip documentation, I haven't found anything >> about how to get numeric char from sscanf. >> >> Since I really don't like to go around to good solutions with the code I >> wrote above (even if it works), does anyone know what the solution is? I= t >> would be helpful for the community to know it. Thanks again to all, I >> really >> appreciate your help. >> >> >> >> >> >> -- >> View this message in context: >> http://microcontrollers.2385.n7.nabble.com/sscanf-crashes-trying-to-read= -multiple-8-bit-char-numbers-in-a-row-tp184339p184399.html >> Sent from the MicroControllers - PIC mailing list archive at Nabble.com. >> -- >> http://www.piclist.com/techref/piclist PIC/SX FAQ & list archive >> View/change your membership options at >> http://mailman.mit.edu/mailman/listinfo/piclist >> > > --=20 http://www.piclist.com/techref/piclist PIC/SX FAQ & list archive View/change your membership options at http://mailman.mit.edu/mailman/listinfo/piclist .