从字符串中获取整数

时间:2014-03-01 15:31:38

标签: c arrays casting

我在从字符串中获取整数并使用它时遇到麻烦。我有:

char *string = "I 17 24 flying bits";

我想将17和24作为整数并使用它们。指针转换是不可能的,我如何获得这些整数并使用它们?

3 个答案:

答案 0 :(得分:3)

int a, b;
int count = sscanf(array, "%*s %d %d", &a, &b);
if (count == 3) // then you matched three tokens, proceed, else failed to match

答案 1 :(得分:1)

您可以使用strtok标准库函数。包含<string.h>标题。

答案 2 :(得分:1)

您可以将strtok与atoi()和isdigit()一起使用。 你可以参考这个link来了解。与可以使用的答案之一相同的逻辑。

相关问题