使用每2个整数的字符串的作为用于方法C中的变量

问题描述 投票:-2回答:1

说我有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);
c
1个回答
0
投票

类似的东西:

#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 $ 
© www.soinside.com 2019 - 2024. All rights reserved.