Problem with parameter passing with arrays

Viewed 85

This is a homework for my C programming class. The program calculates employee gross pay, tax pay, and net take home pay.

The program runs but it automatically ends saying "segmentation fault(core dumped) The error message says:

line 127: warning: passing argument 1 of 'Prompt' from incompatible pointer type

line 13: note: expected 'char*' but argument is of type 'char*(*)[5]'

line 143: warning: passing argument 1 of 'PrintOutput' from incompatible pointer type

line 90: note: expected 'char*' but argument is of type 'char(*)[20][5]'

I don't quite understand the int type because I declared the first_name variable as an array in the main() function. I don't understand the rest of the errors either.

Here is my code:

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

void Prompt(char* a, float* b, float* c)
{
    int i;
    for (i = 0; i < 5; i++) {
        printf("Enter name: ");
        scanf("%s", &a[i]);
        if (strcmp(&a[i],"-1") == 0) {
            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;
        }
    }
    return;
}

void GrossPay(float* grosspay, float* basepay, float* overtimepay,
              float* rate, float* hours)
{
    int i;

    if (hours[i] > 40) {
        overtimepay[i] = (hours[i] - 40) * (rate[i]) * 1.5;
        basepay[i] = rate[i] * hours[i];
        grosspay[i] = rate[i] * hours[i] + (hours[i] - 40) * (rate[i]) * 1.5;
    } else {
        overtimepay[i] = 0;
        basepay[i] = rate[i] * (hours[i]);
        grosspay[i] = basepay[i];
    }
    return;
}

float Taxes(float gross_pay[])
{
    int i;
    float taxes_owed[5];

    for (i = 0; i < 5; i++) {
        taxes_owed[i] = 0.2 * gross_pay[i];
    }
    return taxes_owed[5];
}

float CalculateTotal(float gross_pay[])
{
    int i;
    float total_pay = 0;

    for (i = 0; i < 5; i++) {
        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;
    for (i = 0; i < 5; i++) {
        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\n", net_pay[i]);
    }
    printf("Total paid to all employees= $%5.2f\n", *total_pay);

    return;
}

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


    Prompt(first_name, rate, hours);

    GrossPay(gross_pay, base_pay, overtime_pay, rate, hours);


    taxes_owed[5] = Taxes(gross_pay);

    total_pay = CalculateTotal(gross_pay);


    for (i = 0; i < 5; i++) {
        net_pay[i] = gross_pay[i] - taxes_owed[i];
    }


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

    return 0;
}

Sorry I am very new at coding, never took a class on coding before and I am super confused right now, especially on parameter passing.

1 Answers

When you encountered problems like this first step would be using compiler errors and using Debugger to solve all the syntax error and logical error as well. If you still facing error then you can post in here with source code snippet and logs file if you have any but make sure the question should not posted as duplicate so search before posting.

Now i have solved your code and could be found here on Ideone and here is sample output as well.

Pay to: emp1
Hours worked:  20.0
Hourly rate: $10.00
Gross pay: $200.00 
Base pay: $200.00 
Overtime pay: $ 0.00
Taxes paid: $40.00
Net pay: $160.00


Pay to: emp2
Hours worked:  20.0
Hourly rate: $15.00
Gross pay: $300.00 
Base pay: $300.00 
Overtime pay: $ 0.00
Taxes paid: $60.00
Net pay: $240.00


Pay to: emp3
Hours worked:  25.0
Hourly rate: $15.00
Gross pay: $375.00 
Base pay: $375.00 
Overtime pay: $ 0.00
Taxes paid: $75.00
Net pay: $300.00
Total paid to all employees= $2375.00

And here are some notes about some misconception about array and pointers.

  • char* a is single array but you declared name as 2d array use char** means two level pointer for 2d array.

  • Passing 2d array then you need 2 Indexes i and j for cols and rows respectively.

  • Always initialize local variables with NULL or 0.

  • Pointer and Array in function argument acts same so arr[] and arr*
    are same argument but not in local/global variables.

  • Scanf takes address of variables so we use & to pass address but for arrays we don't do that because Array itself acts as pointer so we
    write scanf("%d",arr+i) to access array with all elements.

  • Always define constants with #define at top of declaration to re-use it in code.

  • Don't try to return local array from function instead pass that as
    argument as pass by reference.

And use pastebin.com or ideone.com to paste your code if its too long in editor to show up.

Related