无效参数已传递给 C 中的函数

Invalid parameter was passed to function in C

提问人:Mohammad Harouak 提问时间:12/21/2020 更新时间:12/21/2020 访问量:557

问:

'粗体行,在开关中案例 3 的第一次扫描中,我收到一个断点错误,内容如下:无效参数已传递给认为无效参数致命的函数。我正在尝试定义一个函数,该函数要求用户提供城市的 ID,然后遍历一个数组,直到找到该 ID 并打印其案例数。请帮忙!!PS:忽略已调用但未定义的函数,我首先要处理每个函数'

#include<stdio.h>
void menu(void);
void filling_arrays(int*, int*, int, int);
int search_cases(int, int*, int*, int);

int main() {
  int cities_id[40], n_cases[40], n, choice, x = 0, ID=0;

  do {
      menu();
      scanf_s("%d", &choice);

      switch (choice) {

      case 1:printf("\nPlease input the number of cities: ");
          scanf_s("%d", &n);
          filling_arrays(cities_id, n_cases, n, x);
          x += n;
          break;
    **case 3:printf("Input the city ID that you would like to know its number of cases: ");
          scanf_s("%d", ID);
          while (search_cases(ID, cities_id, n_cases, x) == -1) {
              printf("Wrong ID!\n");
              printf("Please try again!");
              scanf_s("%d", ID);
          }
          printf("The city with the ID %d has %d cases.",ID, search_cases(ID, cities_id, n_cases, x));
          break;**
      default: printf("Invalid Choice :/\n");
      }
  } while (choice != 5);
  return 0;
}

void menu(void) {
  printf("______________________________________________________\n");
  printf("|________________________Menu_________________________|\n");
  printf("|1. Fill Arrays                                       |\n");
  printf("|2. Display Arrays Content                            |\n");
  printf("|3. Search for the number of cases in a city          |\n");
  printf("|4. Sort the Number of Cases                          |\n");
  printf("|5. Quit                                              |\n");
  printf("|_____________________________________________________|\n");

  printf("\nPlease Make Your Choice: ");
}

void filling_arrays(int* cities_id, int* n_cases, int n, int x) {
  for (int i = 0; i < n; i++) {
      printf("Input a city ID followed with the number of covid19 cases: ");
      scanf_s("%d %d", &cities_id[i + x], &n_cases[i + x]);
  }
}


**int search_cases(int ID, int* cities_id, int* n_cases, int x) {
  int i;
  for (i = 0; i < x; i++) {
      if (cities_id[i] == ID)
          return n_cases[i];
      else
          return -1;
  }
}**
c

评论


答:

0赞 aureliar 12/21/2020 #1

在scanf_s中,您传递的 ID 是一个 int。您应该传递指向 int 的指针。 尝试替换为:

scanf_s("%d", &ID);

评论

0赞 Mohammad Harouak 12/21/2020
我真傻。谢谢,在继续之前,我肯定需要睡一会儿。