c - 通过函数传递结构

标签 c function struct parameter-passing

我想为结构employeeData(小时)的每个数组填写第三个参数。

我想通过询问“请输入员工 x 的工作时间”来提示手动输入工作时间部分,如下所示。虽然它似乎不起作用。

void Get_input (long id_number[], float hours[], int num_empl)
  {
  /*Local Variable Declaration */

int i; /* Variable used in loop counter */

  /* Gets number of employee hours and stores them in an array. */

for (i = 0; i < num_empl; ++i)
  {
    printf("\nEnter the numbers of hours worked by employee # %06li: ",
            employeeData[i].id_number);
    scanf ("%f", &employeeData[i].hours);
  }

 printf("\n\n");
 }  

到目前为止,这是我的完整“程序”:

/*Define and Includes */

 #include <stdio.h>
 #include <cstdlib> 

 /* Define Constants */
#define NUM_EMPL 5
#define OVERTIME_RATE 1.5f
#define STD_WORK_WEEK 40f

/* Define a global structure to pass employee data between functions  */

 struct employee
 {
 long  id_number;
 float wage;
 float hours;
 float overtime;
 float gross;
 };

 /* define prototypes here for each function except main */

 /************************************************************************/
 /*                      Function: Get_input                             */
 /*                                                                      */
 /*  Purpose:    Obtains input from user, the number of hours worked per */
 /*              employee and stores the results in an array that is     */
 /*              passed back to the calling program by reference.        */ 
 /*                                                                      */
 /*  Parameters: clockNum- Array of employee clock numbers.              */
 /*              hours- Array of number of hours worked by an employee   */
 /*              size- Number of employees to process                    */
 /*                                                                      */
 /*  Returns:    Nothing, since 'clockNum' & 'hours' arrays are passed by */
 /*              reference.                                              */
 /************************************************************************/

 void Get_input (long id_number[], float hours[], int num_empl)
 {
  /*Local Variable Declaration */

int i; /* Variable used in loop counter */

  /* Gets number of employee hours and stores them in an array. */

for (i = 0; i < num_empl; ++i)
  {
    printf("\nEnter the numbers of hours worked by employee # %06li: ",
            employeeData[i].id_number);
    scanf ("%f", &employeeData[i].hours);
  }

  printf("\n\n");
  }  

  void Output_results_screen (struct employee [ ], int num_empl);


  /*************************************************************************
  **                      Function: Output_results_screen                  
  **                                                                       
  **  Purpose:    Outputs to screen in a table format the following        
  **                       information about an employee:  Clock, Wage,    
  **                       Hours, Overtime, and Gross Pay.                
  **                                                                       
  **  Parameters:  employeeData - an array of structures containing        
  **                              employee information                     
  **               size - number of employees to process                   
  **                                                                       
  **  Returns:    Nothing (void)                                           
  **                                                                       
   *************************************************************************/

  void Output_results_screen ( struct employee employeeData[], int num_empl )
  {
    int i;    /* loop index */

         printf ("-----------------------------------------\n");   /*Print Header To Screen */
         printf ("Clock# \t Wage \t Hours \t OT \t Gross\n");
         printf ("-----------------------------------------\n");

    /* printf information about each employee */
    for (i = 0; i < num_empl ; ++i)
    {


         printf("%06li    %5.2f    %4.1f   %4.1f   %8.2f    \n", 
                    employeeData[i].id_number, employeeData[i].wage, employeeData[i].hours, 
                    employeeData[i].overtime, employeeData[i].gross);
    } /* for */

    } /* Output_results_screen */


    int main ()
     {


    /* Variable Declaration and initialization */
    struct employee employeeData[NUM_EMPL] = {
    { 98401, 10.60 },
    { 526488, 9.75 },
    { 765349, 10.50 },
    { 34645, 12.25 },
    { 127615, 8.35 }
    };  


     /* Call various functions needed to reading, calculating, and printing as needed */

        /* Function call to get input from user. */
Get_input (id_number, hours, NUM_EMPL);

   /* Function call to output results to the screen in table format. */
    Output_results_screen (employeeData, NUM_EMPL);
    system("pause");  
    return(0);  /* success */

    } /* main */

最佳答案

employeeDatamain 的本地数据,在 GetInput() 中是看不到的,所以我假设您遇到了编译器错误。

将该数组放在顶部所有函数之外(即使其成为全局),它应该可以工作。或者将指向数组的指针传递给 GetInput,以便您可以读入适当的记录。

关于c - 通过函数传递结构,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15193810/

相关文章:

c - 线程和进程之间的文件锁定

python - Python 中缺少必需的位置参数

python - 当我运行这个时,我得到一个 "None"来回答而不是我的问题

c++ - 具有可变数量参数的派生类

c - 根据偏移量和长度设置数组中的 bin 字段

c - C 中的最大数组大小

c - 获取指向窗口像素的指针

结构打印垃圾中的字符数组

arrays - 如何在 Julia 中初始化结构体中的数组?

pointers - 在 Go 中修改结构中的结构 slice