简体   繁体   English

arrays 的参数传递问题

[英]Problem with parameter passing with arrays

This is a homework for my C programming class.这是我的 C 编程 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第 127 行:警告:从不兼容的指针类型传递“提示”的参数 1

line 13: note: expected 'char*' but argument is of type 'char*(*)[5]'第 13 行:注意:预期为 'char*' 但参数类型为 'char*(*)[5]'

line 143: warning: passing argument 1 of 'PrintOutput' from incompatible pointer type第 143 行:警告:从不兼容的指针类型传递“PrintOutput”的参数 1

line 90: note: expected 'char*' but argument is of type 'char(*)[20][5]'第 90 行:注意:预期为 'char*' 但参数的类型为 '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.我不太了解int类型,因为我在main() function 中将first_name变量声明为数组。 I don't understand the rest of the errors either.我也不明白错误的 rest 。

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.抱歉,我对编码很陌生,以前从未在编码上使用过 class,我现在非常困惑,尤其是在参数传递方面。

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.现在我已经解决了你的代码,可以在Ideone上找到,这里也是示例 output。

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 arra y use char** means two level pointer for 2d array . char * a 是单个数组,但您将名称声明为2d数组 y 使用 char** 表示2d 数组的两级指针。

  • Passing 2d array then you need 2 Indexes i and j for cols and rows respectively.传递2d 数组然后您需要2 个索引ij分别用于colsrows

  • Always initialize local variables with NULL or 0 .始终使用NULL0初始化局部变量。

  • Pointer and Array in function argument acts same so arr[] and arr * function 参数中的指针和数组的作用相同,因此arr[]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 Scanf 获取变量的地址,因此我们使用&来传递地址,但对于 arrays 我们不这样做,因为 Array 本身充当指针,所以我们
    write scanf("%d",arr+i) to access array with all elements.编写 scanf("%d",arr+i) 以访问包含所有元素的数组。

  • Always define constants with #define at top of declaration to re-use it in code.始终在声明顶部使用#define定义常量,以便在代码中重用它。

  • Don't try to return local array from function instead pass that as不要尝试从 function 返回本地数组,而是将其传递为
    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.如果代码在编辑器中太长而无法显示,请使用pastebin.comideone.com粘贴您的代码。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM