Two Dimensional Character Arrays

Two-d character arrays are used to create an array of strings. Manipulating a two-d character array is slightly different from manipulating an integer or float two-d array.This is because a character two-d array is likely to be accessed in terms of strings rather than elements, and hence requires only the row subscript . Individual elements can be accessed as usual by specifying both subscripts.

Consider the following example:

#include <stdio.h>

char books[][40] = {

                                 "The Design and Analysis of Algorithms",

                                 "Computer Architecture",

                                 "Prabability and Queuing Theory",

                                 "Operating Systems",

                                 "Analog and Digital Communication",

                                 "Visual Programming"

                               };

main()

{

  int num;

  printf ("\nEnter a semester number (between 1-6)");

  scanf("%d",&num);

  if ( num>=1 && num<=6 )     

  {

    num--; 

/* To account for subscripting from zero */

    printf ("Your book for this semester is %s ", books[num]);

              /*  note the usage of row index to specify the string */

  }

  else

    printf("Wrong semester");

 

 

Copyright © 2008 Manikkumar. All rights reserved.