Thread: passing array structures to functions

  1. #1
    Registered User
    Join Date
    Apr 2003
    Posts
    2

    Unhappy passing array structures to functions

    Having a problem trying to pass an array structure to a function.
    I'm not sure how to properly define the process. I have other
    functions that will need the hours worked passed back too.


    #include <stdio.h>

    #define total_employees 5
    #define over_time_rate 1.5
    #define forty_hour_week 40

    struct employee
    {
    int clock_number ; /* clock # */
    float hourly_rate ; /* hourly rate of pay */
    float hours_worked ; /* hours worked */
    float overtime_hours ; /* OT hours worked */
    float gross_pay ; /* calc gross pay amount */
    float forty_hour_pay ; /* calc gross pay amount for 40 hours */
    float overtime_pay ; /* calc gross pay amount for time > 40 hours */
    };




    struct employee f_input_hours (struct employee[])
    {

    /* local variable declaration */
    int f_counter_one = 0 ; /* variable counter */

    /* populate hours worked array */
    while ( f_counter_one < total_employees )

    /* Prompt for user to input their hours */

    printf ("Please enter the total hours worked for clock
    number ") ;
    printf ("%06i", emp_info_in.clock_number
    [f_counter_one] );
    printf (": ");
    scanf ("%f", &(emp_info_in.hours_worked
    [f_counter_one]));

    /* increment counter */
    ++f_counter_one;

    }

    return(?);

    }

    main()
    {
    /* structure array */
    struct employee emp_info_in[total_employees] =
    { { 98401, 10.60, 0, 0, 0, 0, 0},
    {526488, 9.75, 0, 0, 0, 0, 0},
    {765349, 10.50, 0, 0, 0, 0, 0},
    { 34645, 12.25, 0, 0, 0, 0, 0},
    {127615, 8.35, 0, 0, 0, 0, 0} },

    emp_info_out[total_employees] =
    { { 98401, 10.60, 0, 0, 0, 0, 0},
    {526488, 9.75, 0, 0, 0, 0, 0},
    {765349, 10.50, 0, 0, 0, 0, 0},
    { 34645, 12.25, 0, 0, 0, 0, 0},
    {127615, 8.35, 0, 0, 0, 0, 0} } ;



    /********************** begin performed functions ***************************/

    /* Explain what we will be processing */
    f_explain_message ();


    /* perform get hours function */
    emp_info_out = f_input_hours (emp_info_in
    [total_employees]);

  2. #2
    Registered User Codeplug's Avatar
    Join Date
    Mar 2003
    Posts
    4,981
    There are many things wrong with your code. Study your text and study this code and apply.
    Code:
    struct mystruct_t
    {
        int a,b; //meaningful data
    };
    
    void input_structs(struct mystruct_t ms[],int size)
    {
        int n = 0;
    
        while (n < size)
        {
            printf("Enter a b: ");
    
            scanf("%d %d",&ms[n].a,
                          &ms[n].b);
    
            n++;
        }
    }
    
    int main(void)
    {
        struct mystruct_t data[10];
    
        input_structs(data,10);
    
        printf("\n");
    
        for (int n = 0; n < 10; n++)
        {
            printf("a=%d, b=%d\n",data[n].a,data[n].b);
        }
    
        return 0; //main() always returns int
    }
    Post if you have any other questions.

    Use code tags next time you post code.

    gg
    Last edited by Codeplug; 04-07-2003 at 08:43 PM.

  3. #3
    Registered User
    Join Date
    Apr 2003
    Posts
    2

    Got it

    Thanks, very helpful!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. questions about structures passing through functions
    By nareik9394 in forum C Programming
    Replies: 1
    Last Post: 03-18-2009, 06:19 PM
  2. Passing An Array Of Structures To A Function
    By nexusdarkblue in forum C Programming
    Replies: 3
    Last Post: 03-10-2009, 07:24 AM
  3. array of structures help!
    By voodoo3182 in forum C Programming
    Replies: 12
    Last Post: 08-03-2005, 02:58 PM
  4. Passing an Array of Structures
    By Explicit in forum C Programming
    Replies: 3
    Last Post: 06-03-2004, 07:29 PM
  5. Help Understanding Passing Arrays To Functions
    By jrahhali in forum C++ Programming
    Replies: 7
    Last Post: 04-10-2004, 02:57 PM