beginner question about -- you guessed it -- arrays and pointers
Hi all;
I've been searching archives and playing around with tutorial code for a few hours now so I thought I'd give this forum a try.
My problem is that I'm having a really hard time figuring out how to pass arrays in functions. Specifically, the following code is giving me trouble:
Code:
#ifndef employee_h
#define employee_h
typedef struct emp_struct {
char name[100];
int employee_no;
float salary, tax_to_date;
} Employee;
typedef Employee *Database[10];
int comp_employee (int *database[], int i, int j);
void swap_employee (int *data[], int i, int j);
void init_database(Database employees, int no_employees)
{
**employees[0].name = "Fred" // this gives error of "request for member 'name'
// in something not a structure or union
}
#endif
In the above, pretty much everything except for the **employees[0].name line has been provided as part of the tutorial.
I cannot, for the life of me, figure out how to assign values to the "employees" argument of the init_database method. The attempt I've made above is based on the following logic:
-the Database type represents a 10-element array of pointers to the Employee type.
-Understanding that an array's value is a pointer to its first element: dereferencing "employees" once should produce its first element, which would be a pointer to an Employee type.
-dereferencing "employees" again, then, should yield the actual Employee object, which, I was hoping, could then be assigned. Unfortunately this does not seem to be the case.
Running "sizeof" on "employees", "*employees", and "**employees" seems to confirm my hunch that these items are a pointer, a pointer, and an Employee, respectively. However, I still can't figure out how to make this assignment. Is it just a matter of syntax? Or am I just totally out to lunch with how this array/pointer thing works?
Any help would be very much appreciated. This C thing is a lot harder than Ruby or Java, haha.