C Programming Forum

Ask Question   UnAnswered
Home » Forum » C Programming       RSS Feeds

Searching algorithm of strings with function pointers

  Asked By: Nemanja    Date: Nov 07    Category: C Programming    Views: 1255

Write a function for searching n strings where
the key for searching is the last string from array of strings
(assume all strings are sorted except for the last string).

Prototype of a function is:
int search(int n,char arr[][n],char*(*last_element)(const int,const char**),int(*cmp_strings)(const char*,const char*));

where last_element() returns last string, and cmp_strings() compares two strings.

The following code doesn't work:

#define MAX 10//max num. of characters per string

//return last element from array of strings
char* last_element(const int n,const char arr[][n])
return (const char*)(arr+ n - 1);

//compare two strings
int cmp_strings(const char *a,const char *b)
while(*a && (*a == *b))
return *(const char*)a - *(const char*)b;

int search(int n,char arr[][n],
char*(*last_element)(const int,const char**),
int(*cmp_strings)(const char*,const char*))
int i,j;
for(i=0; i<MAX; )
for(j=0; j<n; j++)
//compare until equal
if((*cmp_strings)((char*)arr[i], (char*)(last_element)((int)n,(char**)arr[i][j])) < 0 ||
(*cmp_strings)((char*)arr[i], (char*)(last_element)((int)n,(char**)arr[i][j])) > 0)
i++;//increment until equal
return i;

int main()
int n,i;
char str[MAX];
while(n < 1);
for(i=0; i<n; i++){
return 0;



No Answers Found. Be the First, To Post Answer.

Didn't find what you were looking for? Find more on Searching algorithm of strings with function pointers Or get search suggestion and latest updates.