Expanding on my comment:
The calc_netSalary
function expects the data in emp
to be laid out like this:
+---+ +-------------+-------+-----------+
emp: | | emp[0] -------> | basicsalary | bonus | netsalary |
+---+ +-------------+-------+-----------+
| | emp[1] ----+
+---+ | +-------------+-------+-----------+
| | emp[2] -+ +--> | basicsalary | bonus | netsalary |
+---+ | +-------------+-------+-----------+
... |
| +-------------+-------+-----------+
+-----> | basicsalary | bonus | netsalary |
+-------------+-------+-----------+
That is, each emp[i]
is a pointer to an instance of struct Employee
.
However, your declaration of emp
in main
looks like this:
+-------------+-------+-----------+
emp: | basicsalary | bonus | netsalary | emp[0]
+-------------+-------+-----------+
| basicsalary | bonus | netsalary | emp[1]
+-------------+-------+-----------+
| basicsalary | bonus | netsalary | emp[2]
+-------------+-------+-----------+
...
That is, each emp[i]
is an instance of struct Employee
.
Either the declaration of emp
in main
is wrong, or the definition of calc_netSalary
is wrong; they cannot be made to work together. To make emp
in main
match up with what calc_netSalary
expects, it needs to be declared an initialized in one of the following ways:
struct Employee **emp = malloc( sizeof *emp * 100 );
if ( emp )
for ( size_t i = 0; i < 100; i++ )
emp[i] = malloc( sizeof *emp[i] );
or
struct Employee *emp[100];
for ( size_t i = 0; i < 100; i++ )
emp[i] = malloc( sizeof *emp[i] );
If emp
is really supposed to be declared as an array of struct Employee
, then the calc_netSalary
function needs to be changed as follows:
int calc_netSalary(struct Employee* emp,int n)
{
int i;
for(i = 0; i < n; i++)
{
emp[i].netsalary = emp[i].basicsalary + emp[i].bonus;
}
return emp[i].netsalary;
}
If you were told to declare emp
as an array of struct Employee
and figure out a way to pass it to calc_netSalary
such that it would be treated as an array of pointers to struct Employee
, then you were given an impossible task. Something is deeply wrong with the assignment.
Edit
Actually, there is a third way around this, but it involves declaring a second array in main
and passing that as the argument:
struct Employee emp[100];
struct Employee *emp2[100];
for ( size_t i = 0; i < 100; i++ )
emp2[i] = &emp[i];
...
calc_netSalary( emp2, 100 );
but I'm assuming the assignment requires you to use emp
.