gpt4 book ai didi

c - C 中方法的段错误

转载 作者:太空宇宙 更新时间:2023-11-04 08:36:56 28 4
gpt4 key购买 nike

#include<stdio.h>
int calc_perc(int r){

float A, B, C, OTHER;
int fullSections, leftover;
const int FULLCLASS = 25;

fullSections = r/FULLCLASS;
leftover = r - (FULLCLASS*fullSections);
A = r*0.30;
B = r*0.25;
C = r*0.15;
OTHER = r*0.30;

printf("\nEnrollment: %d students\n", r);
printf("Full sections: %d\n", fullSections);
printf("Left over: %d students\n", leftover);

printf("\n Students expected to recieve an A: %0.2f ", A);
printf("\n Students expected to recieve a B: %0.2f ", B);
printf("\n Students expected to recieve a C: %0.2f ", C);
printf("\n Students expected to recieve some other grade: %0.2f\n\n", OTHER);

printf("=======================================\n\n");
}

int main(void)
{

int students1, students2, students3;

printf("Elijah Grote\n");
printf("\nEnter three enrollments on one line: ");
scanf("%d %d %d", students1, students2, students3);
calc_perc(students1);
calc_perc(students2);
calc_perc(students3);
return 0;
}

我认为错误发生在 calc_perc 或 scanf 中......但我无法弄清楚是哪个错误以及为什么这样做......编译干净,但是当我输入学生 1、2 和 3 的数字时,它给了我一个段错误。我使用 Unix,当我执行 a.out 并在它要求 3 个数字后输入它时:56 ^H^H 它打印出正确的格式,但没有正确的数字......与错误的内存分配有关或坏指针?

感谢任何帮助,

谢谢

最佳答案

函数 scanf 需要每个目标变量的地址(否则它会如何设置它?)。

改变这个:

scanf("%d %d %d", students1, students2, students3);

对此:

scanf("%d %d %d", &students1, &students2, &students3);

作为旁注,您已声明函数 calc_perc 以返回 int,但它不返回任何内容。

关于c - C 中方法的段错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25828520/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com