#include <stdio.h>
#include <string.h>

void *lsearch(void *key, void *base, int n, int elemSize, int (*cmpfn)(void *, void *))
{
	int i;
	void *elemAddr;

	for(i = 0; i < n; i++)
	{
		elemAddr = (char *)base + i * elemSize;
		if(cmpfn(key, elemAddr) == 0)
			return elemAddr;
	}
	return NULL;
}

int intCmp(void *elem1, void *elem2)
{
	int *ip1 = elem1;
	int *ip2 = elem2;
	return *ip1 - *ip2;
}

int strCmp(void *vp1, void *vp2)
{
	char *s1 = *(char **)vp1;
	char *s2 = *(char **)vp2;
	return strcmp(s1, s2);
}

int main()
{
	int array[] = {1,2,3,4,5,6};
	int size = 6;
	int number = 8;
	int *found;

	found = lsearch(&number, array, size, sizeof(int), intCmp);
	if(found == NULL)
	{
		printf("-_-\n");
	}
	else
	{
		printf("^_^\n");	
	}
	
/*
	char *names[]={"Marvin","Marcus","Skyline","Forward"};  
	char *favorName="Skyline";  
	char **found=lsearch(&favorName, names, 4, sizeof(char *), strCmp);

	if(found == NULL)
	{
		printf("-_-\n");
	}
	else
	{
		printf("^_^\n");	
	}
	printf("sizeof(char) = %d\n", sizeof(char));
	printf("sizeof(int) = %d\n", sizeof(int));
*/
	return 0;
}