C 代码编译良好,但运行时遇到分段错误:11

C 代码编译良好,但运行时遇到分段错误:11,c,C,有人能帮我查一下这个密码吗?。它编译得很好,但当我试着运行它时,它给了我分段错误:11。我不知道我哪里做错了。我怀疑AddDetailToAccumulators函数是它的问题所在,但我似乎找不到缺陷 main.cpp #include <stdio.h> #include <stdlib.h> #include <string.h> #include "function.h" #define OnScreenReport1 "Employee Pay

有人能帮我查一下这个密码吗?。它编译得很好,但当我试着运行它时,它给了我分段错误:11。我不知道我哪里做错了。我怀疑AddDetailToAccumulators函数是它的问题所在,但我似乎找不到缺陷

main.cpp

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "function.h"
#define OnScreenReport1 "Employee     Pay     Reg Hrs  Gross    Fed      SSI    Net  \n"
#define OnScreenReport2 "Name         Rate    Ovt Hrs  Pay      State    Defr   Pay  \n"
#define OnScreenReport3 "========     =====   =======  =======  =======  ====== =====\n"
extern void CalcTax(float Gross, float Deffered, float *FedTax, float *StateTax, float *SSITax);
int main(void)
{
    int numemps;
    float FedTax, StateTax, SSITax,
          hours, payrate, defr, netpay,Gross,
          regHrs, ovtHrs, totreg, totovt,
          totpayR, totgross, totfed, totstate, totssi, totdefr, totnetP;
    char lastname[10], firstname[10], answer = 'y';
    FILE *reportfile;

    PrintReportHeadings(reportfile);
    InitializeAccumulators(&totpayR, &totreg, &totovt, &totgross, &totfed,&totstate, &totssi, &totdefr, &totnetP);
    while (answer == 'y')
     {
         EmployeeData(lastname, firstname, &hours, &payrate, &defr);
          if (hours <= 40)
           {
            regHrs = hours;
           }
          else
           {
            regHrs = 40;
            ovtHrs = hours - 40;
           }
         Gross = CalcGross(hours, payrate);
         CalcTax(Gross, defr, &FedTax, &StateTax, &SSITax);
         netpay = Gross - FedTax - StateTax - SSITax - defr;
         printf(OnScreenReport1);
         printf(OnScreenReport2);
         printf(OnScreenReport3);
         printf("%s, %s     %f     %f     %f     %f     %f      %f\n", lastname, firstname, payrate, regHrs, Gross, FedTax, SSITax, netpay);
         printf("Your reg Hours is %.2f\n", regHrs);
         printf("Your overtime Hours is %.2f\n", ovtHrs);
         printf("Your Gross is: %.2f\n", Gross);
         printf("the Federal Tax is %.2f\n",FedTax);
         printf("the State Tax is %.2f\n",StateTax);
         printf("the SSI Tax is %.2f\n",SSITax);
         AddDetailToAccumulators(payrate, &totpayR, regHrs, &totreg, ovtHrs, &totovt, Gross, &totgross, FedTax, &totfed, StateTax, &totstate, SSITax, &totssi, defr, &totdefr);
         numemps++;
         printf("have more employee? ");
         scanf("%c", &answer);
     }
    PrintSummaryReport(totpayR, totreg, totovt, totgross, totfed, totstate, totssi, totdefr, totnetP, numemps, reportfile);
    return 0;
}
更新:

尽管在这种具体环境下,分段错误似乎是由于其他原因出现的(我错过了指示程序超出数据输入的输出列表),但任何编写符合标准的C代码的人都必须避免

scanf(“%f”,小时)

scanf(“f”,工资率)

scanf(“%f”,defr)

其中,
hours
payrate
defr
指数值变量(浮动)

这被描述为编程错误 我测试了自己,代码

 #include <stdio.h>
 float f = 0.;
 main()
 {
    scanf("%f", f);
    printf("f = %f\n", f);
 }
#包括
浮点数f=0。;
main()
{
scanf(“%f”,f);
printf(“f=%f\n”,f);
}
使用“gcc”会导致SEGFULTS或不读取数据(在不同的框中)


scanf()
需要指向变量的指针,而不是它们的值,尽管在字符串方面没有区别。某些编译器可能会替换流氓代码中的指针,使其与特定编译器一起运行,但此代码是不可移植的。

您是否尝试使用调试器?否。询问您的老师如何使用它或使用web。另外,建议您的老师下一节课应该是关于调试的。这是你必须具备的一项基本技能。必要时,只需打印出一些中间值就可以指出问题所在。第一条规则,初始化所有变量。我不是已经初始化了所有变量吗?一个半体面的编译器通常会对此发出警告。1990年的涡轮增压器C做到了。因此,如果您使用的是比turboc更糟糕的东西,请立即卸载它,然后获得一个新的编译器;这是正确的表格吗?很抱歉,我不太明白,因为我还是新手。@Dat Tran:如果你的课程要求你学习一些C的基础知识,那么你需要理解指针。您还没有。@CoolGuy
&
不需要,因为指针是作为参数传递的。@DatTran-Nebeltruppe和Cool Guy误解了它。
last name?
Teo
first name?
Dg
hours?
30
payrate?
10
defr?
15
Employee     Pay     Reg Hrs  Gross    Fed      SSI    Net  
Name         Rate    Ovt Hrs  Pay      State    Defr   Pay  
========     =====   =======  =======  =======  ====== =====
Teo, Dg     10.000000     30.000000     300.000000     42.750000     22.087500      217.170013
Your reg Hours is 30.00
Your overtime Hours is 0.00
Your Gross is: 300.00
the Federal Tax is 42.75
the State Tax is 2.99
the SSI Tax is 22.09
Segmentation fault: 11
 #include <stdio.h>
 float f = 0.;
 main()
 {
    scanf("%f", f);
    printf("f = %f\n", f);
 }