c - 使用字符串的每 2 个整数作为方法 C 的变量
问题描述
说我有char* b = "2 3, 32 3, 6 8, 9 10"
一个方法randMethod(int x, int y)
我将如何继续遍历将每 2 个 int 作为 randMethod(x, y) 的输入的字符串?
所以它最终会是这样的:
randMethod(2, 3);
randMethod(32, 3);
randMethod(6, 8);
randMethod(9, 10);
解决方案
类似的东西:
#include <stdio.h>
#include <string.h>
void randMethod(int x, int y)
{
printf("%d %d\n", x, y);
}
int main()
{
const char * b = "2 3, 32 3, 6 8, 9 10";
int x, y;
while (sscanf(b, "%d %d", &x, &y) == 2) {
randMethod(x, y);
b = strchr(b, ',');
if (b == NULL)
break;
b += 1;
}
return 0;
}
编译和执行:
pi@raspberrypi:/tmp $ gcc -pedantic -Wextra c.c
pi@raspberrypi:/tmp $ ./a.out
2 3
32 3
6 8
9 10
pi@raspberrypi:/tmp $
推荐阅读
- regex - 如何为 XML 编写正则表达式以删除除 CDATA 之外的未转义的 & 字符?
- listview - Flutter:获取 ListView 大小以创建动态项目大小
- nestjs - NestJS:如何在@Query 对象中转换数组
- python - 在python中从PDF中提取图像
- r - R Shiny:当应用程序中的字段留空时使用函数默认值
- sql - 为什么我不能使用我的服务器名称 (SQL Server)?
- r - 将另一列合并到 rhandsontable
- c - 通过引脚连接时,USB 端口无法识别微控制器
- vba - MS Access VBA 动态显示来自 URL 的图片
- android - 如何添加secp256k1?