我一直收到错误"堆栈粉碎检测到"

use*_*747 -2 c stack-smash

我正在使用C编程并使用gcc进行编译.每次我编译我得到一个堆栈粉碎检测到错误.这是什么意思,我该如何解决?

#include <stdio.h>

#define MAX_ARRAY 50


static int getScore(int assignmentNum[], int weight[], int daysLate[], float score[]){

  int position, tempWeight, tempLate;
  float tempScore;

  scanf("%d %f %d %d", &position, &tempScore, &tempWeight, &tempLate);
  score[position] = tempScore;
  weight[position] = tempWeight;
  daysLate[position] = tempLate;
  assignmentNum[position] = position;
  return weight[position];
}


int main(void){

  int penalty_points,  num_drop, num_assignment;
  static int assignmentNum[MAX_ARRAY], daysLate[MAX_ARRAY], weight[MAX_ARRAY];
  static float score[MAX_ARRAY];
  char statGen;
  int total = 0;

  scanf("%d %d %s", &penalty_points, &num_drop, &statGen);
  printf("%d\n", penalty_points);

  while (total <  100) {
    total = total + getScore(assignmentNum, weight, daysLate, score);
  }    
  return 0;
}
Run Code Online (Sandbox Code Playgroud)

Eri*_*hil 6

您可以使用%s&statGen.该%s说明符是一个字符串; 相应的参数必须指向一个具有足够空间的缓冲区,包括一个终止空字符.但是,statGen是一个单一的char.

要只读一个char,请使用"%c"而不是"%s".如果要在角色前跳过空格,请使用" %c".空间要求scanf跳过空白区域.