Passing an array of structures to a function - C Array

C examples for Array:Array Value

Description

Passing an array of structures to a function

Demo Code

#include <stdio.h>
#define FUNDLEN 50/*w w w  .j  a v a  2 s .  co m*/
#define N 2

struct BackCount {
    char   bank[FUNDLEN];
    double bankfund;
    char   save[FUNDLEN];
    double savefund;
};

double sum(const struct BackCount money[], int n);

int main(void){
    struct BackCount jones[N] = {
        {
            "Bank",1234.27,
            "Loan",4321.94

        },
        {
            "Bank", 2345.56,
            "Time", 3456.78
        }
    };
    
    printf("The total of $%.2f.\n", sum(jones,N));
    
    return 0;
}

double sum(const struct BackCount money[], int n)
{
    double total;
    int i;
    
    for (i = 0, total = 0; i < n; i++)
        total += money[i].bankfund + money[i].savefund;
    
    return(total);
}

Result


Related Tutorials