Searching and displaying a record in Array of Structure in c
-
Hi all, PLease help me for Searching and displaying a record in Array of Structure in c. I think i am meesing up with pointer and array index. PLease help
struct employee{
int emp_id; //empoyee_id int
char emp_name[30];
}emp[MAX];void findAllDetails(int *e){
printf("\n Employee Details...\n");
printf("\n Emp No : %d", emp.emp_id);
printf("\n Emp Name : %s", emp.emp_name);}
void PrintAllDetails(int &K){for(i=0;i
-
Hi all, PLease help me for Searching and displaying a record in Array of Structure in c. I think i am meesing up with pointer and array index. PLease help
struct employee{
int emp_id; //empoyee_id int
char emp_name[30];
}emp[MAX];void findAllDetails(int *e){
printf("\n Employee Details...\n");
printf("\n Emp No : %d", emp.emp_id);
printf("\n Emp Name : %s", emp.emp_name);}
void PrintAllDetails(int &K){for(i=0;i
I would have written something similar to
#include #define EMPLOYEE_ARRAY_SIZE 10
#define NAME_MAX_LENGTH 30
#define NOT_FOUND -1struct employee
{
int id;
char name[NAME_MAX_LENGTH];
};int find_employee_by_id( const struct employee emp_array[], int emp_array_size, int id);
void print_employee( const struct employee * pemp );int main()
{
struct employee emp_array[EMPLOYEE_ARRAY_SIZE] =
{
{ 1, "foo"}, {2, "boo"}, {3, "goo"}, // .. other items here
};int id = 2;
int index = find_employee_by_id( emp_array, EMPLOYEE_ARRAY_SIZE, id );
if ( index != NOT_FOUND )
{
printf("found emplyee details:\n");
print_employee( & emp_array[index] );
}
else
{
printf("employee with id = %d not found\n", id);
}return 0;
}int find_employee_by_id( const struct employee emp_array[], int emp_array_size, int id)
{
int index;
for (index = 0; index < emp_array_size; ++ index)
if ( emp_array[index].id == id)
return index;
return -1;
}void print_employee( const struct employee * pemp )
{
printf("id = %d\n", pemp->id);
printf("name = %s\n", pemp->name);
}"In testa che avete, Signor di Ceprano?" -- Rigoletto