You are trying to use a local variable that ceases to exist when the function returns.
int addEmployee(void) { struct employeelist employee[5]; }
The variable employee exists only inside the addEmployee () function; and every time a function is called, it is a different object.
int printEmployee(int emp) { struct employeelist employee[5]; }
This employee has nothing to do with addEmployee.
But now do not do the easy thing (*); do the right thing : declare an array in the main () function and pass it.
//employee.h struct employeelist { char last [20]; char first[20]; int pnumber; int salary; }; /* add up to `maxemp` employees to the array and ** return number of employees added */ int addEmployee(struct employeelist *, int maxemp); /* print all employees from index 0 to (nemp - 1) */ int printEmployee(struct employeelist *, int nemp);
Declare an array in main () and pass it
#include "employee.h" #include <stdio.h> #include <ctype.h> #include <string.h> int main () { int emp; struct employeelist employee[5]; int emp = addEmployee(employee, 5); printEmployee(employee, emp); return 0; }
(*) Do not declare a global variable for employees
Edited employee.h and added main () function
source share