Hi Everybody,
My mission is to understand basic Abstract data types such as ADT array, linked lists double linked lists stacks etc...
My immediate objective is to make an array of employees to which I can assign values. With that purpose I created a pointer structure which I called *emp_ptr . The idea is that I develop this program to the point where I use linked lists, arrays etc...
Anyway, To make things more challenging, I decided not to have global variables, so I buried the structure in a function which is called employee_create(); However it turns out that despite having tried I still don't understand how to access the values of the structure within the function 'employee_create();' from another function which I called 'employee_data();'
Here is my code:
Code:
/*
* test1.c
*
* Created on: Feb 9, 2010
* Author
*/
#include <stdio.h>
#include <stdlib.h>
//creates an employee
void employee_create() {
//Structure employee
typedef struct employee {
char name[30];
char address[100];
int age;
float salary;
}*emp_ptr[100];
emp_ptr = malloc(sizeof (emp_ptr)); // memory allocation for variable emp - is this right?
}
// Takes input for employee
void employee_data(employee_create(employee *emp_ptr)) { // I thought to pass a //parameter but this of course will not work as this function does not know the //local variables of the employee_create();
printf("Enter the name of the employee: ");
scanf("%s", emp.name); // this will not work because I cannot access the data as it is now
printf("Enter the address of the employee: ");
scanf("%s", emp.address);
printf("Enter the age of the employee: ");
scanf("%d", &emp.age);
printf("Enter the salary of the employee: ");
scanf("%f", &emp.salary);
}
// prints employee
employee print_employee() {
printf("employee information summary:\nName: %s\nAddress: %s\nAge: %d\nSalary: %f\n", emp.name, emp.address, emp.age, emp.salary);
}
int main(void) {
employee_data(); //takes employee data
print_employee(); //prints employee data
printf("The mmeory address of emp is: %p\n", &emp);
return 0;
}
Can somebody point out how to access the structure's values when they are buried inside another function? I thought to use pointers... Am I going through the right direction?