2016-07-23 44 views
0

我不明白为什么每次我尝试输入名称值时都会收到此异常。将函数中的数组传递给scanf时发生异常

这是我得到的异常,Program3.exe中0x0F59B211(ucrtbased.dll)引发的异常:0xC0000005:访问冲突写入位置0xFFFFFFCC。

如果有这种异常的处理程序,程序可能会安全地继续。

我是c新手,请详细解释。

#include <stdio.h> 
#include <string.h> 

int askUser(char name[5][16],float hourlyRate[5],float hoursWorked[5]) 
{ 
    int counter = 0; 

for (int i = 0; i < 5; i++) 
{ 

    printf("enter name: "); 
    scanf_s("%15s", name[i], 16); 
    if (strcmp(name[i], "-1") == 0) 
     break; 
    printf("enter hourly rate: "); 
    scanf_s("%f", &hourlyRate[i]); 
    if (hourlyRate[i] == -1) 
     break; 
    printf("enter hours worked: "); 
    scanf_s("%f", &hoursWorked[i]); 
    if (hoursWorked[i] == -1) 
     break; 

    counter++; 
} 

return counter; 
} 

void main() 
{ 
const float OVERTIMEHOURS = 40.0f; 
const float OVERTIMERATE = 1.5f; 
const float TAX = 0.20f; 
char name[5][16] = { "", "", "", "", "" }; 
float hourlyRate[5] = { 0.0f, 0.0f, 0.0f, 0.0f, 0.0f }; 
float hoursWorked[5] = { 0.0f, 0.0f, 0.0f, 0.0f, 0.0f }; 
float amountPaid[5] = { 0.0f, 0.0f, 0.0f, 0.0f, 0.0f }; 
float basePay[5] = { 0.0f, 0.0f, 0.0f, 0.0f, 0.0f }; 
float overPay[5] = { 0.0f, 0.0f, 0.0f, 0.0f, 0.0f }; 
float taxPaid[5] = { 0.0f, 0.0f, 0.0f, 0.0f, 0.0f }; 
float netPay[5] = { 0.0f, 0.0f, 0.0f, 0.0f, 0.0f }; 
float overTime[5] = { 0.0f, 0.0f, 0.0f, 0.0f, 0.0f }; 
float totalPaid = 0.0f; 

int counter = 0; 


counter = askUser(name[5][16], &hourlyRate[5], &hoursWorked[5]); 
} 
+1

请提供一个能够重现bug的[minimal example](http://stackoverflow.com/help/mcve) – wasthishelpful

+0

在编程生涯的这个阶段,您应该假设如果编译器生成警告,在你的代码中有一个错误。即使在未来几年内,情况仍然会如此之多(100次中的99次)。 –

回答

1

你会错在这里

counter = askUser(name[5][16], &hourlyRate[5], &hoursWorked[5]); 

你的功能askuser需要char[][]类型的第一个参数,但你逝去的char类型的参数,这对于所有传递的参数

因此变为真对askuser()的更正呼叫应该是

counter = askUser(name, hourlyRate, hoursWorked); 

相同的错误重复两次以上主 请在运行代码之前查看编译器警告。