c - How to pass structures to functions whose parameter type has two asterisks -
let there structure called employee :
struct employee { int basicsalary; int bonus; int netsalary; }; let there function prototype follows :
int calc_netsalary(struct employee**,int); let variable array of structures declared in main function follows :
struct employee emp[100]; let function called in main follows :
for (i = 0; < n; i++) { emp[i].netsalary = calc_netsalary(emp[i],n); // warning here } let function definition follows :
int calc_netsalary(struct employee** emp,int n) { int i; for(i = 0; < n; i++) { emp[i]->netsalary = emp[i]->basicsalary + emp[i]->bonus; } return emp[i]->netsalary; } i don't know pass argument function expects parameter 2 asterisks (in place of emp above). getting warning passing argument 1 of ‘calc_netsalary’ incompatible pointer type. know must silly mistake did or not clear concepts of pointers. please !
expanding on comment:
the calc_netsalary function expects data in emp laid out this:
+---+ +-------------+-------+-----------+ emp: | | emp[0] -------> | basicsalary | bonus | netsalary | +---+ +-------------+-------+-----------+ | | emp[1] ----+ +---+ | +-------------+-------+-----------+ | | emp[2] -+ +--> | basicsalary | bonus | netsalary | +---+ | +-------------+-------+-----------+ ... | | +-------------+-------+-----------+ +-----> | basicsalary | bonus | netsalary | +-------------+-------+-----------+ that is, each emp[i] pointer instance of struct employee.
however, declaration of emp in main looks this:
+-------------+-------+-----------+ emp: | basicsalary | bonus | netsalary | emp[0] +-------------+-------+-----------+ | basicsalary | bonus | netsalary | emp[1] +-------------+-------+-----------+ | basicsalary | bonus | netsalary | emp[2] +-------------+-------+-----------+ ... that is, each emp[i] instance of struct employee.
either declaration of emp in main wrong, or definition of calc_netsalary wrong; cannot made work together. make emp in main match calc_netsalary expects, needs declared initialized in 1 of following ways:
struct employee **emp = malloc( sizeof *emp * 100 ); if ( emp ) ( size_t = 0; < 100; i++ ) emp[i] = malloc( sizeof *emp[i] ); or
struct employee *emp[100]; ( size_t = 0; < 100; i++ ) emp[i] = malloc( sizeof *emp[i] ); if emp supposed declared array of struct employee, calc_netsalary function needs changed follows:
int calc_netsalary(struct employee* emp,int n) { int i; for(i = 0; < n; i++) { emp[i].netsalary = emp[i].basicsalary + emp[i].bonus; } return emp[i].netsalary; } if told declare emp array of struct employee , figure out way pass calc_netsalary such treated array of pointers struct employee, given impossible task. wrong assignment.
edit
actually, there third way around this, involves declaring second array in main , passing that argument:
struct employee emp[100]; struct employee *emp2[100]; ( size_t = 0; < 100; i++ ) emp2[i] = &emp[i]; ... calc_netsalary( emp2, 100 ); but i'm assuming assignment requires use emp.
Comments
Post a Comment