简体   繁体   English

将struct数组传递给函数以进行输入

[英]Passing struct array to function for input

I'm working on a program in C language which collects data for the employees such as names, rates and hours. 我正在使用C语言编写程序,该程序为员工收集姓名,费率和工时等数据。 The thing is I need to use struct . 事情是我需要使用struct

I am trying to pass struct array into the load() function where user will put names, hours and rates of the employee but it seems I'm doing it wrong. 我正在尝试将struct数组传递到load()函数中,在该函数中用户将放置员工的姓名,小时和费率,但似乎我做错了。 Can you tell me what is wrong? 你能告诉我怎么了吗?

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define size 3
int i = 0;
struct Employee
{
    char name[20];
    float rate;
    float hours;

}employee;


void load(struct Employee *guys);
void printout();
void edit();
void realedit();
void print();

int main(void)
{   
    int x=0;
    struct Employee guys[size];


    while (x != 5)
    {
        printf("Main Menu\n");
        printf("1. Add Employee\n");
        printf("2. Edit Employee\n");
        printf("3. Print Employee\n");
        Printf("4. Print All Employees\n");
        printf("5. Quit");
        scanf_s("%d", &x);

        switch (x) 
        {
        case 1: load(guys[i]);
            break;


        default: printf("Please enter valid option.\n\n");
            break;
        }
    }


    system("pause");
    return 0;

}

void load(struct Employee *guys)
{
    puts("\ntype name:\n");
    scanf_s("%s", &guys[i]->name, 20);

    puts("\ntype hourly rate:\n");
    scanf_s("%f", &guys[i]->rate);

    puts("\ntype hours worked:\n");
    scanf_s("%f", &guys[i]->hours);


    i++;
}

With the 随着

load(guys[i]);

in your program, you are passing the i th element of the guys array to load() by value. 在您的程序中,您正在按值将guys数组的第i个元素传递给load() So what's being used in load() is actually a copy changes to which will not affect the original value back in main() . 因此, load()中使用的实际上是一个副本更改,该更改不会影响main()的原始值。

Instead you could pass a pointer pointing to the i th element to load() like 相反,您可以将指向第i个元素的指针传递给load()例如

load(&guys[i]);

And use the -> operator to access the variables inside the struct . 并使用->运算符访问struct内部的变量。

void load(struct Employee *guys)
{
    puts("\ntype name:\n");
    scanf_s("%s", guys->name, 20);

    puts("\ntype hourly rate:\n");
    scanf_s("%f", guys->rate);

    puts("\ntype hours worked:\n");
    scanf_s("%f", guys->hours);
}

And no need to use [i] for indexing as we take care of elements one by one. 而且,由于我们一一处理元素,因此无需使用[i]进行索引。


In

scanf_s("%s", &guys[i]->name, 20);

the function expects the base address of the array. 该函数需要数组的基地址。 So use 所以用

scanf_s("%s", guys[i]->name, 20);

as the name of the array decays into a pointer to the first element of the array. 因为数组名称衰减为指向数组第一个元素的指针。

As for your use of system() in 至于你在使用system()

system("pause");

consider reading Why should the system() function be avoided in C and C++? 考虑阅读为什么在C和C ++中应避免使用system()函数? .

Also, note that puts() will automatically print a \\n at the end. 另外,请注意puts()将在末尾自动打印\\n So, if you see fit you can do away with a \\n . 因此,如果您认为合适,可以取消\\n

And you have a small typo. 而且你有一个小的错字。 The

Printf("4. Print All Employees\n");

should've been 应该是

printf("4. Print All Employees\n");

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

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