简体   繁体   中英

Working with atoi

I have been attacking atoi from several different angles trying to extract ints from a string 1 digit at a time.

Problem 1 - Sizing the array
Should this array of 50 chars be of size 50 or 51 (to account for null terminator)?

char fiftyNumbersOne[51] = "37107287533902102798797998220837590246510135740250";

Problem 2 - atoi output

What am I doing wrong here?

char fiftyNumbersOne[51] = "37107287533902102798797998220837590246510135740250";
int one = 0;
char aChar = fiftyNumbersOne[48];
printf("%c\n",aChar);//outputs 5 (second to last #)
one = atoi(&aChar);
printf("%d\n",one);//outputs what appears to be INT_MAX...I want 5

Problem 1

The array should be length 51. But you can avoid having to manually figure that out by simply doing char fiftyNumbersOne[] = "blahblahblah"; .

Problem 2

aChar is not a pointer to the original string ; it's just an isolated char floating about in memory somewhere. But atoi(&aChar) is treating it as if it were a pointer to a null-terminated string. It's simply walking through memory until it happens to find a 0 somewhere, and then interpreting everything it's found as a string.

You probably want:

one = aChar - '0';

This relies on the fact that the character values for 0 to 9 are guaranteed to be contiguous.

  1. 51.
  2. That's because aChar is not null-terminated. If you just want to get the integer value of a char , simply use

    one = aChar - '0';

Problem 1 - Sizing the array Should this array of 50 chars be of size 50 or 51 (to account for null terminator)?

You always want an array one bigger than what you need to store in it (to account for the null terminator). So your 50 chars should be stored in an array of size 51.

What am I doing wrong here?

Try null terminating your input string to atoi. Documentation says atoi is supposed to be given the pointer to a string - which is different than a non-terminated single character. Your results with the current code you posted vary on different platforms (I get -1 on unbuntu/gcc).

char fiftyNumbersOne[51] = "37107287533902102798797998220837590246510135740250";
int one = 0;
char aChar = fiftyNumbersOne[48];
char intChar[2];
printf("%c\n",aChar);//outputs 5 (second to last #)
sprintf(intChar, "%c", aChar); //print the char to a null terminated string
one = atoi(&intChar);
printf("%d\n",one);//outputs what appears to be INT_MAX...I want 5

Should this array of 50 chars be of size 50 or 51 (to account for null terminator)?

51, but you can also declare it without size.

char foo[] = "foo";

What am I doing wrong here?

Not reading the documentation for atoi I guess. aChar is a char , so you're passing the right type to atoi , but atoi is expecting this type to represent a string of characters, normally terminated by the character '\0'. Your "string" isn't terminated.

One solution to this is

char aString[2];
aString[0] = fiftyNumbersOne[48];
aString[1] = '\0';
atoi(aString);

Another is doing fiftyNumbersOne[48] - '0' instead of calling atoi , since in ASCII the decimal codes are consecutive and increasing from 0 to 9.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM