Search for the position of a name in the array using a recursive binary search

Posted on 2004-11-14
Last Modified: 2010-04-15
I have written a program that takes an array of twenty strings, sorts then in ascending ASCII order using selection sort and then searches for the position of the name in the array using a recursive binary search.  Upon compiling I receive the following error message: warning: passing arg 3 of `search' from incompatible pointer type.  Can you please help me determine the correct pointer type for the program to compile?  The program is to read the strings from a file but I have initalized the array within main for the time being.

The program is as follows:

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

#define NUMWORDS 20
#define LENWORDS 25

void displayName (void);
int search (int start, int end, char *words [], const char s[]);

//Main controls the

int main (int argc, char *argv[])
   FILE *infptr;
   char words [NUMWORDS][LENWORDS] =
   int i,j,k;
   int currMin=0;
   char temp[NUMWORDS];
   int result;
   char searchWord [LENWORDS];
   if (argc <= 1)
     printf("\nNot enough command line arguments.\n");
   infptr = fopen (argv [1], "r");
   if ((infptr)==NULL)
      printf("\nERROR: Unable to open data for reading.\n\n");
   if ((argc>=2) && ((infptr)!=NULL))
      displayName ();
      for (j=0; j < 19; j++)
         for (k=j+1; k < 20; k++)
            if (strcmp(words[currMin], words[k])>0)
         strcpy(temp, words[currMin]);
         strcpy(words[currMin], words [j]);
      for (i=0; i < NUMWORDS; i++)
         printf ("\n%d: %s", i, words[i]);
         printf ("\n\n");

         printf("Please enter a word to search (or quit to stop): ");
         scanf ("%s", searchWord);
         result = search (0, NUMWORDS - 1, words, searchWord);
         printf("The word \"%s\"", searchWord);
         if (result == -1)
            printf ("was not found.\n");
         printf ("was found at position %d.\n", result);
   //Format and finish.
   printf ("\n");
   return 0;

//The name of the programmer is displayed.
void displayName (void)
   printf("\nName: Chandra Parr\n\n");

int search (int start, int end, char *words [], const char s[])
   int middle, cmpRes;
   if (start > end)
      return (-1);
   else if (start == end)
      if (strcmp(words[start], s) == 0)
         return start;
         return (-1);
      middle = (start + end) / 2;
      cmpRes = strcmp(words[middle],s);
      if (cmpRes == 0)
         return middle;
      else if (cmpRes > 0)
         return search(start, middle - 1, words, s);
         return search (middle + 1, end, words, s);
Question by:csunnypl2
    1 Comment
    LVL 3

    Accepted Solution


    You'll get better responses on this if you post it in the "C Programming" section.


    Featured Post

    How to run any project with ease

    Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
    - Combine task lists, docs, spreadsheets, and chat in one
    - View and edit from mobile/offline
    - Cut down on emails

    Join & Write a Comment

    Introduction Hi all and welcome to my first article on Experts Exchange. A while ago, someone asked me if i could do some tutorials on object oriented programming. I decided to do them on C#. Now you may ask me, why's that? Well, one of the re…
    It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
    how to add IIS SMTP to handle application/Scanner relays into office 365.
    Access reports are powerful and flexible. Learn how to create a query and then a grouped report using the wizard. Modify the report design after the wizard is done to make it look better. There will be another video to explain how to put the final p…

    745 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    14 Experts available now in Live!

    Get 1:1 Help Now