I would have written something similar to
#include #define EMPLOYEE_ARRAY_SIZE 10
#define NAME_MAX_LENGTH 30
#define NOT_FOUND -1
struct 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