Hi all,
I have been trying to make a program to test pointers and structures amongst other things. I have tried to reduce it to the minimal expression because this is where the problem is. The code consists on a structure, three functions (one of which is just a menu).
The two important functions are
employee create();
print_employee();
The objective now is to create an dynamic array of employees and then output the information for a particular employee which I choose by pointing to it with a pointer.
All works fine including memory allocation except that when I, for example, choose to enter two employee records and then later I want to output employee id number one, I still get the output for the last employee entered, that is, the output for employee number two. I think there is something wrong either with the variables I call or with the pointers.
Can anybody hep? code below...
Thank you,
bluetxxth
Code:
#include <stdio.h>
#include <stdlib.h>
//Structure - can I not bury this inside the a function? how to access it later...?
typedef struct employee {
int emp_index; //To assign a value to each employee for a later "pointing"
char name[30];
char address[100];
int age;
float salary;
}employee;
// Takes imput that establishes the size of the staff
int staff_size(int emp_num) {
printf("What is the size of your staff?: ");
scanf("%d", &emp_num);
return emp_num;
}
//Takes empoyee data input from user
employee_create(int emp_num, employee *emp_rec, employee **emp_id) {
printf("\nEnter employee id: ");
scanf("%d", &emp_rec[emp_num].emp_index);
printf("\nEnter the name of the employee: ");
scanf("%s",emp_rec[emp_num].name);
printf("\nEnter the address of the employee: ");
scanf("%s", emp_rec[emp_num].address);
printf("\nEnter the age of the employee: ");
scanf("%d", &emp_rec[emp_num].age);
printf("\nEnter the salary of the employee: ");
scanf("%f", &emp_rec[emp_num].salary);
//this is an array of id which takes emp_index as the index which in turn is a pointer which has a pointer with an index as its index and which accesses the emp_index value
emp_id[emp_rec[emp_num].emp_index] = &emp_rec[emp_num];
}
void print_employee(employee **emp_id , int emp_num) {
printf("Enter employee you want to inspect: ");
scanf("%d",&emp_num);
printf("\nEmployee summary: Id: %d \nName: %s \nAddress: %s \nAge: %d \nSalary: %f", emp_id[emp_num]-> emp_index, emp_id[emp_num]->name, emp_id[emp_num]->address, emp_id[emp_num]->age, emp_id[emp_num]->salary);//Prints desired employee
}
void menu() {
printf("----------------------------------------\n");
printf("|Choose amongst the following options: |\n");
printf("----------------------------------------\n");
printf("| 1- Enter employee data: |\n");
printf("| 2- inspect employees: |\n");
printf("----------------------------------------\n\n");
}
int main(void) {
int d = 0, emp_num =0, k;
int option;
employee *emp_rec;
employee **emp_id;
employee emp_struct; // Structure declaration
emp_rec = malloc (sizeof(emp_struct)*emp_num); // dynamic memory allocation for an array of records
emp_id = malloc (sizeof(emp_struct)*emp_num);// dynamic memory allocation for an array of employee ids
//generate menu
menu();
while (option != 0) {
switch (option) {
case 1: printf("\nEnter employee data: ");
for(d = 0; d < staff_size(emp_num) ; d++){
employee_create(emp_num, emp_rec, emp_id);
break;
}break;
case 2: printf("\nInspect employee: ");
print_employee(emp_id, k);
break;
}
printf("\nSelect an option: ");
scanf("%d", &option); //Returns to default state
//exit while loop
}
free(emp_rec);
free(emp_id);
return (0);
}