与阿托伊合作

发布于 2024-11-04 11:04:02 字数 595 浏览 4 评论 0原文

我一直在从几个不同的角度攻击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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

夏末的微笑 2024-11-11 11:04:02

问题 1

数组的长度应为 51。但是您只需执行 char FifthNumbersOne[] = "blahblahblah"; 即可避免手动计算该长度。

问题2

aChar 不是指向原始字符串的指针;它只是一个漂浮在内存中某处的孤立的char。但是atoi(&aChar)将其视为指向空终止字符串的指针。它只是简单地遍历内存,直到碰巧在某处找到 0 ,然后将找到的所有内容解释为字符串。

您可能想要:

one = aChar - '0';

这依赖于 09 的字符值保证是连续的。

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.

掩饰不了的爱 2024-11-11 11:04:02
  1. 51.
  2. 那是因为 aChar 不是以 null 终止的。如果您只想获取 char 的整数值,只需使用

    one = aChar - '0';
    
  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';
    
扛刀软妹 2024-11-11 11:04:02

问题 1 - 调整数组大小应该
这个 50 个字符的数组的大小为 50
或 51(以解决空
终结者)?

您总是需要一个比您需要存储的数组大一的数组(以考虑空终止符)。所以你的 50 个字符应该存储在一个大小为 51 的数组中。

我在这里做错了什么?

尝试使用 null 终止 atoi 的输入字符串。文档说 atoi 应该被赋予指向字符串的指针 - 这与非终止的单个字符不同。您发布的当前代码的结果在不同平台上有所不同(我在 unbuntu/gcc 上得到 -1)。

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

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
梦行七里 2024-11-11 11:04:02

这个 50 个字符的数组的大小应该是 50 还是 51(以考虑空终止符)?

51,但也可以声明不带大小。

char foo[] = "foo";

我在这里做错了什么?

我猜没有阅读 atoi 的文档。 aChar 是一个 char,因此您将正确的类型传递给 atoi,但 atoi 需要此类型表示一个字符串,通常以字符“\0”结尾。你的“字符串”没有终止。

解决此问题的一种

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

方法是执行 fiftyNumbersOne[48] - '0' 而不是调用 atoi,因为在 ASCII 中,十进制代码是连续的,并且从 0 增加到 9。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文