我正在嘗試編寫一個跟蹤員工工資單的程序,我似乎可以通過引用正確傳遞參數。執行程序時,我無法越過因爲自動出現斷點的用戶輸入功能。我不斷收到第一個函數中的線程1 EXC_BAD_ACCESS斷點
#include <stdio.h>
#define size 5
void userinput(int *hoursworked[size], float *rate[size], char *name[size]);
float calculatepay(float *rate[size], int *hoursworked[size]);
float tax(float *grosspay[size]);
float totalpay(float *grosspay[size]);
void print(char *name[size][20], float *rate[size], int *hoursworked[size], float *grosspay[size], float sum[size], float taxespaid[size]);
int main()
{
int hoursworked[size];
float rate[size];
char name[size];
float grosspay[size];
float taxespaid[size];
float sum[size];
userinput(&hoursworked[size], &rate[size], &name[size]);
calculatepay(&rate[size], &hoursworked[size]);
tax(&grosspay[size]);
totalpay(&grosspay[size]);
print(&name[size], &rate[size], &hoursworked[size], &grosspay[size], &sum[size], &taxespaid[size]);
return 0;
}
void userinput(int *hoursworked[size], float *rate[size], char *name[size])
{
for(int i=0; i<size; i++){
printf("Enter name: ");
scanf("%s", name[i] , 20);
printf("Enter hours worked: ");
scanf("%d", hoursworked[i]);
printf("Enter hourly rate: ");
scanf("%f", rate[i]);
}
}
float calculatepay(float *rate[size], int *hoursworked[size])
{
float grosspay[size];
for(int i=0; i<size; i++){
grosspay[size] = *rate[size] * *hoursworked[size];
}
return grosspay[size];
}
float tax(float *grosspay[size])
{
float taxespaid[size];
for(int i=0; i<size; i++){
taxespaid[size] = *grosspay[size]*0.2;
}
return taxespaid[size];
}
float totalpay(float *grosspay[size])
{
float sum;
int i;
for (i=0; i<size; i++) {
sum = sum + *grosspay[i];
}
return(sum);
}
void print(char *name[size][20], float *rate[size], int *hoursworked[size], float *grosspay[size], float sum[size], float taxespaid[size])
{
for(int i=0; i<size; i++){
printf("Pay to: %s", name[i][20]);
printf("Hours worked: %d", hoursworked[i]);
printf("Hourly rate: %f", rate[i]);
printf("Gross Pay: %f", grosspay[i]);
printf("Taxes paid: %f", taxespaid[i]);
printf("Net pay: %f", *grosspay[i] - taxespaid[i]);
printf("Total paid to all employees: %f", sum[i]);
}
}
您對如何傳遞數組完全感到困惑。你沒有書嗎?這比你做得更容易。擺脫函數聲明中的星號。去掉通話中的&符號。數組自動作爲指針傳遞。順便說一句,你應該把'size'全部大寫:'SIZE'。這是定義常量的慣例。 – ooga