简体   繁体   中英

Returning and printing string array index in C

I've got a function that searches through a list of names and I'm trying to get the search function to return the index of the array back to the main function and print out the starting location of the name found. Everything I've tried up to this point either crashes the program or results in strange output.

Here is my search function:

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

#define MAX_NAMELENGTH 10
#define MAX_NAMES 5

void initialize(char names[MAX_NAMES][MAX_NAMELENGTH], int Number_entrys, int i);
int search(char names[MAX_NAMES][MAX_NAMELENGTH], int Number_entrys);

int main()
{
   char names[MAX_NAMES][MAX_NAMELENGTH];
   int i, Number_entrys,search_result,x;
   printf("How many names would you like to enter to the list?\n");
   scanf("%d",&Number_entrys);
   initialize(names,Number_entrys,i);
   search_result= search(names,Number_entrys);
   if (search_result==-1){
      printf("Found no names.\n");
   }else
   {
      printf("%s",search_result);
   }
   getch();
   return 0;
}

void initialize(char names[MAX_NAMES][MAX_NAMELENGTH],int Number_entrys,int i)
{
   if(Number_entrys>MAX_NAMES){
      printf("Please choose a smaller entry\n");
   }else{
      for (i=0; i<Number_entrys;i++){
         scanf("%s",names[i]);
      }
   }
}

int search(char names[MAX_NAMES][MAX_NAMELENGTH],int Number_entrys)
{
   int x;
   char new_name[MAX_NAMELENGTH];
   printf("Now enter a name in which you would like to search the list for\n");
   scanf("%s",new_name);

   for(x = 0; x < Number_entrys; x++) {
      if ( strcmp( new_name, names[x] ) == 0 )
      {
         return x;
      }
   } 
   return -1;        
}

Like I mentioned before I have tried a lot of different ways to try and fix this issue, but I cant seem to get them to work. Printing X like what I have above is just the last thing I tried, and therefor know that it doesn't work. Any suggestions on the simplest way to do this?

Why don't you use strcmp instead of strstr ?

In your code it seems that there is some huge issues : -it seems that i is use not initialize. -You declare x as a int and then use : printf("%s",x) it's kind of a non-sense here. And by the way not initiliaze !

Something like that should be better (Do note that I haven't your initialize function) and I haven't try to compile :

int search(char names[MAX_NAMES][MAX_NAMELENGTH],int Number_entrys)
{
   int x =0;
   char new_name[MAX_NAMELENGTH];
   printf("Now enter a name in which you would like to search the list for\n");
   scanf("%s",new_name);

   for(x = 0; x < Number_entrys; x++) 
   {
      if ( strcmp( new_name, names[x] ) == 0 )
      {
         return x;
      }
   } 
   return -1;        
 }

MAIN :

int main()
{
    char names[MAX_NAMES][MAX_NAMELENGTH];
    int i=0;
    int Number_entrys=0;
    int search_result=0;
    printf("How many names would you like to enter to the list?\n");
    scanf("%d",&Number_entrys);
    initialize(names,Number_entrys,i); // I guess it is use to initialize names ?!?
    search_result= search(names,Number_entrys);
    if (search_result==-1)
    {
         printf("Found no names.\n");
    }
    else
    {
       printf("Index found in position %d in the tab\n",search_result);
    }
   getch(); //not really a fan of this...
   return 0;
 }

Hope it helps.

Regards,

Joze

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM