Ok, generics and C. Working on a small testing API. Pretty early I stumbled across this problem. Lecture 4 of Programming Paradigms discussed the issue and gave some nice examples. This is a version which lets you see if two arrays are identical.
The basic idea is that you can compare addresses in memory with a function called memcmp(). You can loop through a generic array by knowing the size of the elements(bits and bytes :p) and by knowing the address(pointer) in which they are located.
void match(void key, void *base, int n, int elm_size);int *m; int n = 2; int a[2] = {5, 4}; int b[2] = {5, 4};
if((m = match(&b, &a, n, sizeof(int))) != NULL){ printf("m equals: %dn", *m); }
// do a linear search on an array of which we dont know the type. // return the element if there is a match. void match(void key, void base, int n, int elm_size) { int i, j; for(i = 0; i < n; i++) { void key_addr = (char )key + i * elm_size; void elm_addr = (char *)base + i * elm_size; if(memcmp(key_addr, elm_addr, elm_size) != 0) return elm_addr; } return NULL; }