说我有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 $

10-06 16:12