Create an array of pointers to strings. - C String

C examples for String:String Array

Description

Create an array of pointers to strings.

Demo Code

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

char  *dic[][40] = {
   "a", "A.",
   "b", "B",
   "c", "C",
   "d", "D",
   "", ""  /* null terminate the list */
};

int main(void)
{
   char word[80], ch;
   char **p;/*from  www  .j ava 2  s.  co  m*/

   do {
      puts("\nEnter a letter: ");
      scanf("%s", word);

      p = (char **)dic;

      /* find matching word and print its meaning */
      do {
         if (!strcmp(*p, word)) {
            puts("Meaning:");
            puts(*(p + 1));
            break;
         }
         if (!strcmp(*p, word)) break;
         p = p + 2; /* advance through the list */
      } while (*p);
      if (!*p) puts("Word not in dictionary.");
      printf("Another?  (y/n): ");
      scanf(" %c%*c", &ch);
   } while (toupper(ch) != 'N');

   return 0;
}

Result


Related Tutorials