You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

C语言员工薪资计算程序数组参数传递错误及段错误求助

Hey there! Let's break down your issues step by step—since you're new to programming, I'll keep things clear and avoid overly technical jargon where I can.

First: Understanding the "Incompatible Pointer Type" Warnings

Those warnings are all about mismatched data types when you pass variables to your functions. Let's unpack each one:

Warning 1: Prompt function argument mismatch

Your Prompt function is declared to take a char* (a pointer to a single character) as its first parameter, but in main you're passing first_name—a 2D array: char first_name[20][5] (20 names, each up to 4 characters plus a null terminator).

When you pass a 2D array to a function, it doesn't become a char*—it becomes a pointer to a 5-character array (written as char (*a)[5]). Update the Prompt function's first parameter to match this type; you can write it either as:

void Prompt(char a[][5], float* b, float* c)

or explicitly as a pointer:

void Prompt(char (*a)[5], float* b, float* c)

Both work the same way for accessing array elements.

Warning 2: PrintOutput function argument mismatch

Your PrintOutput function expects a char first_name[5] (a single 5-character array) but you're passing &first_name—a pointer to the entire 20x5 array (type char (*)[20][5]). Fix this by updating the PrintOutput parameter to accept a 2D array:

void PrintOutput(char first_name[][5], float* rate, float* hours, float* gross_pay, float* base_pay, float* overtime_pay, float* taxes_owed, float* net_pay, float* total_pay)

And when you call it in main, just pass first_name (not &first_name):

PrintOutput(first_name, rate, hours, gross_pay, base_pay, overtime_pay, taxes_owed, net_pay, &total_pay);

Next: Fixing the Segmentation Fault (Core Dumped)

Segfaults happen when your program tries to access memory it shouldn't. Let's fix the biggest culprits:

1. Uninitialized variable in GrossPay

Your GrossPay function uses an int i but never sets its value! i will be a random number, so accessing hours[i] reads way outside the array bounds. Add a loop to iterate through each employee, and stop early if the user entered -1:

void GrossPay(float* grosspay, float* basepay, float* overtimepay, float* rate, float* hours) {
    int i;
    for (i = 0; i < 5; i++) {
        if (rate[i] == -1) break;
        
        if (hours[i] > 40) {
            overtimepay[i] = (hours[i] - 40) * rate[i] * 1.5;
            basepay[i] = rate[i] * 40; // Base pay is only for regular 40 hours!
            grosspay[i] = basepay[i] + overtimepay[i];
        } else {
            overtimepay[i] = 0;
            basepay[i] = rate[i] * hours[i];
            grosspay[i] = basepay[i];
        }
    }
}

I also fixed the base pay calculation—you were multiplying rate by total hours even for overtime, which is incorrect (base pay should be 40 hours at regular rate, overtime is extra).

2. Broken Taxes function

Right now, your Taxes function has two critical issues:

  • You declare a local array taxes_owed[5] inside the function, then try to return taxes_owed[5]—but array indices start at 0, so taxes_owed[5] is out of bounds (accessing memory beyond the array).
  • Local arrays are destroyed when the function finishes, so returning values from them gives garbage data.

Instead, pass the taxes_owed array from main into the function to fill it directly:

void Taxes(float gross_pay[], float taxes_owed[]) {
    int i;
    for (i = 0; i < 5; i++) {
        if (gross_pay[i] == 0 && i > 0 && gross_pay[i-1] == 0) break;
        taxes_owed[i] = 0.2 * gross_pay[i];
    }
}

Call it in main like this (instead of assigning to taxes_owed[5]):

Taxes(gross_pay, taxes_owed);

3. Incorrect scanf in Prompt

In Prompt, you have scanf("%s", &a[i])—but a[i] is already a 5-character array, and array names act like pointers to their first element. Using &a[i] gives a pointer to the entire array (type char (*)[5]), which doesn't match what %s expects. Change it to:

scanf("%4s", a[i]);

The %4s ensures we don't exceed the 5-character array limit (leaving space for the null terminator \0).

4. Out-of-bounds array access in main

You had taxes_owed[5] = Taxes(gross_pay);—but taxes_owed only has indices 0-4, so taxes_owed[5] is out of bounds. We fixed this by reworking the Taxes function to take the array as a parameter.

Quick Primer on Parameter Passing for Arrays (For Newbies)

Let's simplify this since you're confused about it:

  • 1D arrays: When you pass a 1D array (like float rate[5]) to a function, it converts to a pointer to the first element (so float*). That's why you can use b[i] in Prompt to access the i-th element of rate.
  • 2D arrays: A 2D array (like char first_name[20][5]) is an array of arrays. When passed to a function, it becomes a pointer to the first sub-array (so char (*)[5]). You must specify the sub-array size (the 5 here) so the compiler knows how to calculate memory offsets for a[i][j].
  • Local arrays are temporary: Never return a local array from a function—once the function ends, that memory is freed, and any pointers to it become invalid. Always pass the array from the caller into the function if you need to fill it with data.

Fixed Full Code

Here's the complete code with all fixes applied:

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

void Prompt(char a[][5], float* b, float* c) {
    int i;
    for (i = 0; i < 5; i++) {
        printf("Enter name (or -1 to stop): ");
        scanf("%4s", a[i]);
        if (strcmp(a[i],"-1") == 0) {
            b[i] = -1;
            break;
        }
        printf("Enter hourly rate: ");
        scanf("%f", &b[i]);
        if (b[i] == -1) {
            break;
        }
        printf("Enter hours worked: ");
        scanf("%f", &c[i]);
        if (c[i] == -1) {
            break;
        }
    }
}

void GrossPay(float* grosspay, float* basepay, float* overtimepay, float* rate, float* hours) {
    int i;
    for (i = 0; i < 5; i++) {
        if (rate[i] == -1) break;
        
        if (hours[i] > 40) {
            overtimepay[i] = (hours[i] - 40) * rate[i] * 1.5;
            basepay[i] = rate[i] * 40;
            grosspay[i] = basepay[i] + overtimepay[i];
        } else {
            overtimepay[i] = 0;
            basepay[i] = rate[i] * hours[i];
            grosspay[i] = basepay[i];
        }
    }
}

void Taxes(float gross_pay[], float taxes_owed[]) {
    int i;
    for (i = 0; i < 5; i++) {
        if (gross_pay[i] == 0 && i > 0 && gross_pay[i-1] == 0) break;
        taxes_owed[i] = 0.2 * gross_pay[i];
    }
}

float CalculateTotal(float gross_pay[]) {
    int i;
    float total_pay = 0;
    for (i = 0; i < 5; i++) {
        if (gross_pay[i] == 0 && i > 0 && gross_pay[i-1] == 0) break;
        total_pay += gross_pay[i];
    }
    return total_pay;
}

void PrintOutput(char first_name[][5], float* rate, float* hours, float* gross_pay, float* base_pay, float* overtime_pay, float* taxes_owed, float* net_pay, float* total_pay) {
    int i;
    printf("\n--- Employee Payroll ---\n");
    for (i = 0; i < 5; i++) {
        if (rate[i] == -1) break;
        
        printf("\nPay to: %s\n", first_name[i]);
        printf("Hours worked: %5.1f\n", hours[i]);
        printf("Hourly rate: $%5.2f\n", rate[i]);
        printf("Gross pay: $%5.2f \n", gross_pay[i]);
        printf("Base pay: $%5.2f \n", base_pay[i]);
        printf("Overtime pay: $%5.2f\n", overtime_pay[i]);
        printf("Taxes paid: $%5.2f\n", taxes_owed[i]);
        printf("Net pay: $%5.2f\n", net_pay[i]);
        printf("------------------------\n");
    }
    printf("\nTotal paid to all employees= $%5.2f\n", *total_pay);
}

int main() {
    char first_name[20][5];
    float rate[5] = {0};
    float hours[5] = {0};
    float gross_pay[5] = {0};
    float base_pay[5] = {0};
    float overtime_pay[5] = {0};
    float taxes_owed[5] = {0};
    float net_pay[5] = {0};
    float total_pay;
    int i;

    Prompt(first_name, rate, hours);
    GrossPay(gross_pay, base_pay, overtime_pay, rate, hours);
    Taxes(gross_pay, taxes_owed);
    
    for (i = 0; i < 5; i++) {
        if (rate[i] == -1) break;
        net_pay[i] = gross_pay[i] - taxes_owed[i];
    }
    
    total_pay = CalculateTotal(gross_pay);
    PrintOutput(first_name, rate, hours, gross_pay, base_pay, overtime_pay, taxes_owed, net_pay, &total_pay);

    return 0;
}

I added extra touches like initializing arrays to 0, clear stop conditions for -1 input, and formatted output for readability.

内容的提问来源于stack exchange,提问作者Delaney

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.08 15:57:33