2D character array-String array-Declaration and initialization

In this C Programming tutorial, we will discuss 2D character arrays in detail and will discuss how to declare, initialize and use 2D character arrays or String arrays.

1. What is 2D character array in C?

We have successfully learned the basic concepts and different library functions that C Programming offers. Another interesting concept is the use of 2D character arrays. In the previous tutorial, we already saw that string is nothing but an array of characters that ends with a ‘\0’.

2D character arrays are very similar to 2D integer arrays. We store the elements and perform other operations in a similar manner. A 2D character array is more like a String array. It allows us to store multiple strings under the same name.


2. How to Declaration and Initialization a 2D character array?

A 2D character array is declared in the following manner:

char name[5][10];

The order of the subscripts is important during declaration. The first subscript [5] represents the number of Strings that we want our array to contain and the second subscript [10] represents the length of each String. This is static memory allocation. We are giving 5*10=50 memory locations for the array elements to be stored in the array.

Initialization of the character array occurs in this manner:

char name[5][10]={
                   "tree",
                   "bowl",
                   "hat",
                   "mice",
                   "toon"
                 };

Let’s see the diagram below to understand how the elements are stored in the memory location:

2D char array

The areas marked in green show the memory locations that are reserved for the array but are not used by the string. Each character occupies 1 byte of storage from the memory.


3. How to take 2D array Data input from user?

In order to take string data input from the user we need to follow the following syntax:

for(i=0 ;i<5 ;i++ )
scanf("%s",&name[i][0]);

Here we see that the second subscript remains [0]. This is because it shows the length of the string and before entering any string the length of the string is 0.


4. Printing the array elements

The way a 2D character array is printed is not the same as a 2D integer array. This is because we see that all the spaces in the array are not occupied by the string entered by the user.

If we display it in the same way as a 2D integer array we will get unnecessary garbage values in unoccupied spaces. Here is how we can display all the string elements:

for(i=0 ;i<5 ;i++)
printf("%s\n",name[i]);

This format will print only the string contained in the index numbers specified and eliminate any garbage values after ‘\0’. All the string library functions that we have come across in the previous tutorials can be used for the operations on strings contained in the string array. Each string can be referred to in this form:

name[i][0];

where [i] is the index number of the string that needs to be accessed by library functions.


5. Program to search for a string in the string array

Let’s implement a program to search for a string(a char array) entered by the user in a 2D char array or a string array(also entered by the user):

#include <stdio.h>
#include <string.h>
int main() {
  char name[5][10],
      item[10]; // declaring the string array and the character
                // array that will contain the string to be matched
  int i, x, f = 0;

  /*taking 5 string inputs*/
  printf("Enter 5 strings:\n");
  for (i = 0; i < 5; i++)
    scanf("%s", &name[i][0]);

  /*entering the item to be found in the string array*/
  printf("Enter the string to be searched:\n");
  scanf("%s", &item);

  /*process for finding the item in the string array*/
  for (i = 0; i < 5; i++) {
    x = strcmp(&name[i][0],
               item); // compares the string in the array with the item and if
                      // match is found returns 0 and stores it in variable x
    if (x == 0)
      f = i;
  }

  /*if match is not found*/
  if (f == 0)
    printf("the item does not match any name in the list");
  /*If the match is found*/
  else
    printf("Item Found. Item in the array exists at index - %d", f);
  return 0;
}

Output:-
Enter 5 strings:
tree
bowl
hat
mice
toon
Enter the string to be searched:
mice
Item Found. Item in the array exists at index - 3

Helpful Links

Please follow C Programming tutorials or the menu in the sidebar for the complete tutorial series.

Also for the example C programs please refer to C Programming Examples.

All examples are hosted on Github.


Recommended Books


An investment in knowledge always pays the best interest. I hope you like the tutorial. Do come back for more because learning paves way for a better understanding

Do not forget to share and Subscribe.

Happy coding!! ?

Recommended -

Subscribe
Notify of
guest
0 Comments
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x
Index