c - 在 C 中返回和打印字符串数组索引

标签 c printing indexing

我有一个搜索名称列表的函数,我试图让搜索函数将数组的索引返回给主函数,并打印出找到的名称的起始位置。到目前为止,我所做的所有尝试要么使程序崩溃,要么导致奇怪的输出。

这是我的搜索功能:

#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;        
}

就像我之前提到的,我尝试了很多不同的方法来尝试解决这个问题,但我似乎无法让它们起作用。像上面那样打印 X 只是我尝试的最后一件事,因此知道它不起作用。关于最简单的方法有什么建议吗?

最佳答案

为什么不使用 strcmp 而不是 strstr ?

在您的代码中似乎存在一些大问题: - 看来我是在使用而不是初始化。 - 你将 x 声明为一个 int 然后使用: printf("%s",x) 在这里有点胡说八道。顺便说一句,不要初始化!

类似的东西应该更好(请注意我没有你的初始化函数)并且我没有尝试编译:

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;        
 }

主要内容:

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;
 }

希望对您有所帮助。

问候,

乔泽

关于c - 在 C 中返回和打印字符串数组索引,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13147515/

相关文章:

c++ - 无用的反斜杠会产生定义明确的字符串常量吗?

python - 计算DataFrame中的词频

MySQL : Applying index to benifit query with 4 user controlled conditions in a where clause

python - numpy 数组与 nan 与标量的不等式比较

c - 指向全局函数的局部指针

c - 绘制分段线,其中线段基于变量着色

c - 为什么编译器看到的是 'unsigned int' 而不是函数?

Windows:如何告诉打印机在打印过程中发出 FormFeed?

python - 在 python 中打印具有格式的混合类型字典

html - 使用打印媒体 css 打印 div 内容