struct cd {
    char *name;
    double population;
};


int search_get_index_by_name(const char *name, const struct cd *data, const size_t data_length,
        int (*cmp_func)(const void *, const void *))
{
    struct cd key = { (char *) name, 0 };
    struct cd *match = bsearch(&key, data, data_length,
            sizeof(struct cd), cmp_func);

    if (match == NULL)
        return -1;
    else
        return ((intptr_t) match - (intptr_t) data) / sizeof(struct cd);
}


double search_get_pop_by_name(const char *name, const struct cd *data, size_t data_length,
        int (*cmp_func)(const void *, const void *))
{
    struct cd key = { (char *) name, 0 };
    struct cd *match = lfind(&key, data, &data_length,
            sizeof(struct cd), cmp_func);

    if (match == NULL)
        return -1;
    else
        return match->population;
}


char* search_get_pop_threshold(double pop_threshold, const struct cd *data, size_t data_length,
        int (*cmp_func)(const void *, const void *))
{
    struct cd key = { NULL, pop_threshold };
    struct cd *match = lfind(&key, data, &data_length,
            sizeof(struct cd), cmp_func);

    if (match == NULL)
        return NULL;
    else
        return match->name;
}

int cd_nameChar_cmp(const void *a, const void *b)
{
    struct cd *aa = (struct cd *) a;
    struct cd *bb = (struct cd *) b;

	int i,len = strlen(aa->name);

	for(i=0;i<len;i++)
		if(bb->name[i]!=aa->name[i])
			return -1;
	return 0;
}

int cd_name_cmp(const void *a, const void *b)
{
    struct cd *aa = (struct cd *) a;
    struct cd *bb = (struct cd *) b;
    return strcmp(bb->name, aa->name);
}

int cd_pop_cmp(const void *a, const void *b)
{
    struct cd *aa = (struct cd *) a;
    struct cd *bb = (struct cd *) b;
    return bb->population >= aa->population;
}

int main(void)
{
    const struct cd citydata[] = {
        { , 21 },
        { , 15.2 },
        { , 11.3 },
        { , 7.55 },
        { , 5.85 },
        { , 4.98 },
        { , 4.7 },
        { , 4.58 },
        { , 4.4 },
        { , 3.98 }
    };

    const size_t citydata_length = LEN(citydata);

    printf(, search_get_index_by_name(, citydata, citydata_length, cd_name_cmp));
    printf(, search_get_pop_threshold(5, citydata, citydata_length, cd_pop_cmp));
    printf(, search_get_pop_by_name(, citydata, citydata_length, cd_nameChar_cmp));

    return 0;
}