How to pass nested struct to function in C

1 Answer

0 votes
#include <stdio.h>
 
typedef struct
{
   int data;
} info;

typedef struct
{
   info age;
   float salary;
} worker;
 
void print_struct(worker w);
 
int main(void)
{
    worker w;
    
    printf("Enter Age: ");
    scanf("%d", &w.age.data);
    printf("Enter Salary: ");
    scanf("%f", &w.salary);

    print_struct(w);
 
    return 0;
}

void print_struct(worker w)
{
    printf("age = %d\n", w.age.data);
    printf("salary = %.2f\n", w.salary);
}

/*
run:
 
Enter Age: 43
Enter Salary: 5473
age = 43
salary = 5473.00

*/


answered Oct 8, 2014 by avibootz

Related questions

3 answers 1,611 views
1 answer 124 views
1 answer 70 views
1 answer 337 views
1 answer 139 views
1 answer 176 views
...