与阿托伊合作
我一直在从几个不同的角度攻击atoi,试图从字符串中一次提取一位数字的整数。
问题 1 - 调整数组大小
这个 50 个字符的数组的大小应该是 50 还是 51(以考虑空终止符)?
char fiftyNumbersOne[51] = "37107287533902102798797998220837590246510135740250";
问题 2 - atoi 输出
我在这里做错了什么?
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
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
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
问题 1
数组的长度应为 51。但是您只需执行
char FifthNumbersOne[] = "blahblahblah";
即可避免手动计算该长度。问题2
aChar
不是指向原始字符串的指针;它只是一个漂浮在内存中某处的孤立的char
。但是atoi(&aChar)将其视为指向空终止字符串的指针。它只是简单地遍历内存,直到碰巧在某处找到0
,然后将找到的所有内容解释为字符串。您可能想要:
这依赖于
0
到9
的字符值保证是连续的。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 isolatedchar
floating about in memory somewhere. Butatoi(&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 a0
somewhere, and then interpreting everything it's found as a string.You probably want:
This relies on the fact that the character values for
0
to9
are guaranteed to be contiguous.那是因为
aChar
不是以 null 终止的。如果您只想获取char
的整数值,只需使用That's because
aChar
is not null-terminated. If you just want to get the integer value of achar
, simply use您总是需要一个比您需要存储的数组大一的数组(以考虑空终止符)。所以你的 50 个字符应该存储在一个大小为 51 的数组中。
尝试使用 null 终止 atoi 的输入字符串。文档说 atoi 应该被赋予指向字符串的指针 - 这与非终止的单个字符不同。您发布的当前代码的结果在不同平台上有所不同(我在 unbuntu/gcc 上得到 -1)。
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.
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) .
51,但也可以声明不带大小。
我猜没有阅读 atoi 的文档。
aChar
是一个char
,因此您将正确的类型传递给atoi
,但atoi
需要此类型表示一个字符串,通常以字符“\0”结尾。你的“字符串”没有终止。解决此问题的一种
方法是执行
fiftyNumbersOne[48] - '0'
而不是调用atoi
,因为在 ASCII 中,十进制代码是连续的,并且从 0 增加到 9。51, but you can also declare it without size.
Not reading the documentation for
atoi
I guess.aChar
is achar
, so you're passing the right type toatoi
, butatoi
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
Another is doing
fiftyNumbersOne[48] - '0'
instead of callingatoi
, since in ASCII the decimal codes are consecutive and increasing from 0 to 9.