How to pass array of pointers to structures using functions in C?
#include<stdio.h>
#define dept_count 3
struct employee
{
char name[20],
post[20];
int emp_no;
int basic_pay;
};
void search( struct employee * , char , int );
void main()
{
struct employee *deptt[dept_count];
int i,j,emp_count[10],dep_srch;
char emp_srch[20];
clrscr();
for(i = 0 ; i < dept_count ; i ++)
{
printf("\nenter the number of employees in dept %d : ",i+1);
scanf("%d",&emp_count[i]);
for(j = 0 ; j < emp_count[i] ; j ++)
{
printf("\nEnter Employee Number : ");
scanf("%d",&deptt[i]->emp_no);
printf("\nEnter Employee's name : ");
scanf("%s",deptt[i]->name);
printf("\nEnter Employees post: ");
scanf("%s",deptt[i]->post);
printf("\nEnter Employees basic pay: ");
scanf("%d",&deptt[i]->basic_pay);
}
}
for(i = 0 ; i < dept_count ; i ++)
{
printf("\n\t\tDepartment %d: \n\n", i+1);
printf("\n\nEmp_no ");
printf("\tName ");
printf("\tPost ");
printf("\tBasic pay ");
for(j = 0 ; j < emp_count[i] ; j ++)
{
printf("\n\n%d",deptt[i]->emp_no);
printf("\t%s",deptt[i]->name);
printf("\t%s",deptt[i]->post);
printf("\t%d",deptt[i]->basic_pay);
}
}
printf("\n\nEnter the Deptt. No in which employee is to be searched:");
scanf("%d",&dep_srch);
printf("\nEnter the employee name:");
scanf("%s",emp_srch);
search(deptt[dep_srch],emp_srch[20],emp_count[dep_srch]);
getch();
}
void search (struct employee &dept , char name[], int emp_count)
{
int i,j;
for(i= 0 ; i < emp_count ; i++)
{
j = 0;
while(dept[i] == name[j] && name[j] != '\0')
{
j++;
}
if(name[j]=='\0')
{
printf("\n\tString found at index %d",i+1);
break;
}
}
if(name[j]!='\0')
printf("\n\tString not found....");
}