Use a null pointer to mark the end of a pointer array. - C Pointer

C examples for Pointer:NULL Pointer

Description

Use a null pointer to mark the end of a pointer array.

Demo Code

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

int search(char *p[], char *name);

char *names[] = { "A","B","C","D",NULL }; /* null pointer constant ends the list */

int main(void)
{
   if (search(names, "D") != -1)
      printf("D is in list.\n");

   if (search(names, "B") == -1)
      printf("B not found.\n");

   return 0;/*from  w w w  . j  a va2 s. c o m*/
}

/* Look up a name. */
int search(char *p[], char *name)
{
   register int t;

   for (t = 0; p[t]; ++t)
      if (!strcmp(p[t], name)) return t;

   return -1; /* not found */
}

Result


Related Tutorials