lsearch.c

来自「vc library 韩国语版 希望对大家又帮助」· C语言 代码 · 共 42 行

C
42
字号
/* lsearch example */

#include <stdlib.h>
#include <stdio.h>
#include <string.h>     /* for strcmp declaration */

/* initialize number of colors */
char *colors[10] = { "Red", "Blue", "Green" };
int ncolors = 3;

int colorscmp(char **arg1, char **arg2)
{
   return(strcmp(*arg1, *arg2));
}

int addelem(char *key)
{
   int oldn = ncolors;
   lsearch(key, colors, (size_t *)&ncolors, sizeof(char *),
     (int(*)(const void *,const void *))colorscmp);
   return(ncolors == oldn);
}

int main(void)
{
   int i;
   char *key = "Purple";

   if (addelem(key))
      printf("%s already in colors table\n", key);
   else
   {
      strcpy(colors[ncolors-1],key);
      printf("%s added to colors table\n", key);
   }

   printf("The colors:\n");
   for (i = 0; i < ncolors; i++)
      printf("%s\n", colors[i]);
   return 0;
}

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?