Coder123
Coder123

Reputation: 854

bsearch() comparing by name

I have a struct employee comparators and main:

#define MAX_SIZE 20
typedef struct Employee{
    char name[MAX_SIZE];
    int salary;
    int experience;
} employee_t;

void main()
{
    int i;

    employee_t** employeeArray = (employee_t**)malloc(sizeof(employee_t*)*5);
    employee_t nonExstingEmployee = {"v"};

    for(i=0 ; i < 2 ; i++)
    {
        employeeArray[i] = (employee_t*)malloc(sizeof(employee_t));  //alocate each employee
        readEmployee( employeeArray[i] ); 
    }

    puts("beforesort\n");

    for(i=0; i <2 ; i++)
    {
        printEmployee(employeeArray[i]);
    }

    puts("after salary sort\n");
    qsort(employeeArray, 2, sizeof(employee_t*), compareEmployeesBySalary);
    for(i=0; i < 2 ; i++)
        printEmployee( employeeArray[i] );

    if (bsearch(&nonExstingEmployee, employeeArray, MAX_SIZE, sizeof(employee_t), compareEmployeesByName) == 0)
        puts("employee found");
    else
        puts("no employee found");

}


    int compareEmployeesBySalary(const void* a, const void* b){
            employee_t* one = *(employee_t **)a;
            employee_t* two = *(employee_t **)b;

            if (one->salary == two->salary)
                return 0;
            else if (one->salary > two->salary)
                return 1;
            else
                return -1;
    }

    int compareEmployeesByName(const void* a,const void* b)
    {
        employee_t* one = *(employee_t**)a;
        employee_t* two = *(employee_t**)b;

        if(strcmp(&one->name, &two->name) == 0)
        {
            return 1;
        }

        return 0;
    }

I need to use bsearch comparing the names (with some employee that does not exist in the array), my qsort function works(comparing by salary) no matter how I initialize the employee for bsearch it always says its not there (I'm trying to test this with existing employee first),

How should I initialise my employee (or what should I change?)


[update from comment:]

This is my readEmployee():

void readEmployee(employee_t *emp)
{
  fflush(stdin);
  printf("Please enter Employee's name:(max 19 chars)");
  scanf("%s", emp->name);

  fflush(stdin);
  printf("Please enter Employee's salary:");
  scanf("%d", &(emp->salary));

  fflush(stdin);
  printf("Please enter Employee's experience:");
  scanf("%d", &(emp->experience));
}

I'm comparing the nonExstingEmployee (which exist in the array for testing) and my employeeArray.

Upvotes: 3

Views: 92

Answers (2)

Matt
Matt

Reputation: 15091

The problem is simple: you qsort by salary yet bsearch by name. It won't work.

bsearch performs binary search in sorted array. Not any sorted array but an array sorted according to the search criteria. You must either previously perform qsort by name or stick to lsearch (that is linear search which doesn't depend upon previous sorting).

Upvotes: 2

alk
alk

Reputation: 70941

How should I initialise my employee

You might want to show us how you do it (readEmployee()?), and we would comment on this.

(or what should I change?)

Two issues:

  1. The array to be searched is not MAX_SIZE long, but 2.

    So this line

    if (bsearch(&nonExstingEmployee, employeeArray, MAX_SIZE, sizeof(employee_t), compareEmployeesByName) == 0)
    

    should be

    if (bsearch(&nonExstingEmployee, employeeArray, 2, sizeof(employee_t), compareEmployeesByName) == 0)
    
  2. In compareEmployeesByName() you pass the "wrong" address.

    This line

    if(strcmp(&one->name, &two->name) == 0)
    

    should be

    if(strcmp(one->name, two->name) == 0)
    

Upvotes: 1

Related Questions