1.1.4 Declaring Two-Dimensional character array

  • we can also form the array of strings as like the array of numbers.
  • List of fruits, list of vegetables are examples of the array of strings.
  • These can be represented in C using two-dimensional character arrays. The size of the row is the number of strings and size of the column is the length of the each string plus 1.(i.e. ‘\0’ null character)
  • To store the name of five fruits with longest fruit name of 9 character, following declaration is used.
  • char fruits[5][10]; 
  • The array fruits is initialized as follows:
  • char fruits[5][10]={ “apple”, “grape” , “pineapple” , “banana”, “orange” };
  • The size in above is not compulsory. If not given, compiler calculates it.
  • To access particular fruit name from  fruit array, say ith fruit name, we can use fruit[i]
  • char fruit[5][10] occupies  total 50 bytes. Each fruit name occupies 10 bytes. 

Leave a Comment

Your email address will not be published. Required fields are marked *