我正在用 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;
}
最佳答案
您将 %s
与 &statGen
一起使用。 %s
说明符用于字符串;相应的参数必须指向一个具有足够空间容纳字符串的缓冲区,包括终止空字符。但是,statGen
是单个char
。
要仅读取单个字符
,请使用"%c"
而不是"%s"
。如果要跳过字符前的空格,请使用"%c"
。空格要求 scanf
跳过空格。
关于c - 我不断收到错误 "stack smashing detected",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19052863/