我有一个大小为30(char array [30]
)的字符数组。它只有一个元素,其中包含整数数据类型。如何将该数组元素转换为C中的整数元素?
任何帮助将不胜感激!
答案 0 :(得分:1)
如果我正确理解你的问题:
if
#include <stdio.h>
int main()
{
int i = atoi( " 3 ");
printf( "%d", i );
return 0;
}
会在找到一个数字之前忽略任何空白区域,并在检测到非数字字符时停止。
答案 1 :(得分:0)
评论几乎说明了所有内容,并为您提供了有关atoi
,sscanf
和strtol
的详细信息。这里我有一些示例字符串以及这些函数返回的结果。由于不每个字符串都成功转换,因此我添加了strtol
的扩展实现。生成此输出的代码也在下面给出see it online in action at ideone。
atoi:以非数字开头的字符串计算为0
。根本没有给出错误信息。
0: atoi with "3" to integer: +3
1: atoi with " 3 " to integer: +3
2: atoi with " -3 " to integer: -3
3: atoi with "str 3 " to integer: +0
4: atoi with "str-3 " to integer: +0
5: atoi with " 3str" to integer: +3
6: atoi with " -3str" to integer: -3
7: atoi with "str-3str" to integer: +0
8: atoi with "s-r-3str" to integer: +0
sscanf:仅使用%d
调用:此处相同,但将匹配的项目作为计数返回,因此0
将指示错误。
0: sscanf with "3" to integer: +3 | itemCount = 1
1: sscanf with " 3 " to integer: +3 | itemCount = 1
2: sscanf with " -3 " to integer: -3 | itemCount = 1
3: sscanf with "str 3 " to integer: +0 | itemCount = 0 --> Error!
4: sscanf with "str-3 " to integer: +0 | itemCount = 0 --> Error!
5: sscanf with " 3str" to integer: +3 | itemCount = 1
6: sscanf with " -3str" to integer: -3 | itemCount = 1
7: sscanf with "str-3str" to integer: +0 | itemCount = 0 --> Error!
8: sscanf with "s-r-3str" to integer: +0 | itemCount = 0 --> Error!
strtol:此处相同,但将范围溢出的错误代码返回errno
。此外,endPtr
是为您提供有关转化的更多详细信息的参数之一。
0: strtol with "3" to integer: +3 | errno = 0, StartPtr = 0x7ffc47e9a140, EndPtr = 0x7ffc47e9a141, PtrDiff = 1
1: strtol with " 3 " to integer: +3 | errno = 0, StartPtr = 0x7ffc47e9a130, EndPtr = 0x7ffc47e9a135, PtrDiff = 5
2: strtol with " -3 " to integer: -3 | errno = 0, StartPtr = 0x7ffc47e9a120, EndPtr = 0x7ffc47e9a125, PtrDiff = 5
3: strtol with "str 3 " to integer: +0 | errno = 0, StartPtr = 0x7ffc47e9a110, EndPtr = 0x7ffc47e9a110, PtrDiff = 0 --> Error!
4: strtol with "str-3 " to integer: +0 | errno = 0, StartPtr = 0x7ffc47e9a100, EndPtr = 0x7ffc47e9a100, PtrDiff = 0 --> Error!
5: strtol with " 3str" to integer: +3 | errno = 0, StartPtr = 0x7ffc47e9a0f0, EndPtr = 0x7ffc47e9a0f5, PtrDiff = 5
6: strtol with " -3str" to integer: -3 | errno = 0, StartPtr = 0x7ffc47e9a0e0, EndPtr = 0x7ffc47e9a0e5, PtrDiff = 5
7: strtol with "str-3str" to integer: +0 | errno = 0, StartPtr = 0x7ffc47e9a0d0, EndPtr = 0x7ffc47e9a0d0, PtrDiff = 0 --> Error!
8: strtol with "s-r-3str" to integer: +0 | errno = 0, StartPtr = 0x7ffc47e9a0c0, EndPtr = 0x7ffc47e9a0c0, PtrDiff = 0 --> Error!
以下是我的strtol
函数的代码,以便您可以解析更多不同的字符串。 my_strtol
的使用与strtol
的用法完全相同。如果你有像"str-str-3"
之类的字符串,还需要一个辅助函数,因为第一个减号-
不是整数。
my_strtol()实施:
/* Position exactly to integer beginning */
/* e.g. if you have: "str-str-3str " */
/* the returning pointer will point to: */
/* "-3str". */
const char* getNumberPos(const char* str)
{
const char* curChar = str;
/* Iterate through part of integer is there. */
while ( '\0' != curChar
&& !(isdigit(*curChar) || '+' == *curChar || '-' == *curChar))
{
++curChar;
}
/* Check if the sign is part of an integer, */
/* if not use this function recursive! */
if ('+' == *curChar || '-' == *curChar)
{
const char* nextChar = curChar + 1;
if ( '\0' != *nextChar
&& !isdigit(*nextChar))
{
++curChar;
curChar = getNumberPos(curChar);
}
}
return curChar;
}
/* Extended strtol function */
long my_strtol(const char* str, char** endptr, int base)
{
const char* curChar = getNumberPos(str);
return strtol(curChar, endptr, base);
}
my_strtol的结果: my_strtol
函数适用于上面给出的所有字符串:
0: my_strtol with "3" to integer: +3 | errno = 0, StartPtr = 0x7ffc47e9a140, EndPtr = 0x7ffc47e9a141, PtrDiff = 1
1: my_strtol with " 3 " to integer: +3 | errno = 0, StartPtr = 0x7ffc47e9a130, EndPtr = 0x7ffc47e9a135, PtrDiff = 5
2: my_strtol with " -3 " to integer: -3 | errno = 0, StartPtr = 0x7ffc47e9a120, EndPtr = 0x7ffc47e9a125, PtrDiff = 5
3: my_strtol with "str 3 " to integer: +3 | errno = 0, StartPtr = 0x7ffc47e9a110, EndPtr = 0x7ffc47e9a115, PtrDiff = 5
4: my_strtol with "str-3 " to integer: -3 | errno = 0, StartPtr = 0x7ffc47e9a100, EndPtr = 0x7ffc47e9a105, PtrDiff = 5
5: my_strtol with " 3str" to integer: +3 | errno = 0, StartPtr = 0x7ffc47e9a0f0, EndPtr = 0x7ffc47e9a0f5, PtrDiff = 5
6: my_strtol with " -3str" to integer: -3 | errno = 0, StartPtr = 0x7ffc47e9a0e0, EndPtr = 0x7ffc47e9a0e5, PtrDiff = 5
7: my_strtol with "str-3str" to integer: -3 | errno = 0, StartPtr = 0x7ffc47e9a0d0, EndPtr = 0x7ffc47e9a0d5, PtrDiff = 5
8: my_strtol with "s-r-3str" to integer: -3 | errno = 0, StartPtr = 0x7ffc47e9a0c0, EndPtr = 0x7ffc47e9a0c5, PtrDiff = 5
以下代码显示的是为所有四个函数生成输出的测试代码。
#include <stdio.h>
#include <stdlib.h>
#include <stddef.h>
#include <ctype.h>
#include <string.h>
#include <errno.h>
int main()
{
/* Loop index */
int idx;
/* Declare and initialize char arrays. */
char array[] = { "3" };
char arrayI[] = { " 3 " };
char arrayII[] = { " -3 " };
char arrayIII[] = { "str 3 " };
char arrayIV[] = { "str-3 " };
char arrayV[] = { " 3str" };
char arrayVI[] = { " -3str" };
char arrayVII[] = { "str-3str" };
char arrayVIII[] = { "s-r-3str" };
/* Save each array in one array for iteration purposes. */
const char* const arrays[] = { array,
arrayI,
arrayII,
arrayIII,
arrayIV,
arrayV,
arrayVI,
arrayVII,
arrayVIII };
/* Get size of that array. */
const int arraysSize = sizeof(arrays) / sizeof(char*);
/* atoi(): Does not detect errors! */
for (idx = 0; idx < arraysSize; ++idx)
{
int integer = atoi(arrays[idx]);
printf("%d: atoi with \"%s\" %*s to integer: %+d\n",
idx,
arrays[idx],
8 - strlen (arrays[idx]),
"",
integer);
}
printf("\n");
/* sscanf(): Error detection by using the matched item count. */
for (idx = 0; idx < arraysSize; ++idx)
{
int integer = 0;
int itemCount = sscanf(arrays[idx], "%d", &integer);
printf("%d: sscanf with \"%s\" %*s to integer: %+d | itemCount = %d",
idx,
arrays[idx],
8 - strlen (arrays[idx]),
"",
integer,
itemCount);
if (0 == itemCount)
{
printf(" --> Error!");
}
printf("\n");
}
printf("\n");
/* strtol(): Error detection by using errno and returned end pointer. */
for (idx = 0; idx < arraysSize; ++idx)
{
char* endPtr = NULL;
ptrdiff_t ptrDiff = 0;
errno = 0;
/* You have to check if the long can be stored in the int, not done here! */
int integer = (int) strtol(arrays[idx], &endPtr, 10);
ptrDiff = endPtr - arrays[idx];
printf("%d: strtol with \"%s\" %*s to integer: %+d | errno = %d, StartPtr = %p, EndPtr = %p, PtrDiff = %ld",
idx,
arrays[idx],
8 - strlen (arrays[idx]),
"",
integer,
errno,
(void*) arrays[idx],
(void*) endPtr,
ptrDiff);
if (0 != errno || 0 == ptrDiff)
{
printf(" --> Error!");
}
printf("\n");
}
printf("\n");
/* my_strtol: Same as strtol here. */
for (idx = 0; idx < arraysSize; ++idx)
{
char* endPtr = NULL;
ptrdiff_t ptrDiff = 0;
errno = 0;
/* You have to check if the long can be stored in the int, not done here! */
int integer = (int) my_strtol(arrays[idx], &endPtr, 10);
ptrDiff = endPtr - arrays[idx];
printf("%d: my_strtol with \"%s\" %*s to integer: %+d | errno = %d, StartPtr = %p, EndPtr = %p, PtrDiff = %ld",
idx,
arrays[idx],
8 - strlen (arrays[idx]),
"",
integer,
errno,
(void*) arrays[idx],
(void*) endPtr,
ptrDiff);
if (0 != errno || 0 == ptrDiff)
{
printf(" --> Error!");
}
printf("\n");
}
return 0;
}