2-Dimensional Character Array.

  • As like 2-D array of int type has to specify the number of rows and columns.
  • In 2-D character array,we have to specify the number of strings and the number of character in string.

Declaration 2-D character array

Syntax:

char array_name[no_of_strings][no_of_characters];

Lets declare some 2-d character array.

#include<stdio.h>

void main()
{   // this char array has space of storing 3 string
    // with maximum 10 character in  each string.
    char char1[3][10];
    
    
    // this char array has space of storing 4 string
    // with maximum 10 character in  each string.
    char char2[4][10];
    
    
    // this char array has space of storing 5 string
    // with maximum 10 character in  each string.
    char char3[5][10];
}


Initializing 2-D char array.

Syntax:

char array_name[no_of_strings][no_of_character] ={"string1","string2","string3",.....,...};

Let's initialize some 2-D char array.

#include<stdio.h>

void main()
{   // this char array has space of storing 3 string
    // with maximum 15 character in  each string.
    char devices[3][15] ={"Computer","Laptop","Cell Phone"};


    // this char array has space of storing 4 string
    // with maximum 15 character in  each string.
    char Dreams[4][15] = {"Programmer","Engineer","Doctor","Actor"};


    // this char array has space of storing 5 string
    // with maximum 15 character in  each string.
    char games[5][15] = {"Football","Cricket","Basketball","Baseball","Tennis"};
}


Accessing Elements

  • In Array ,every element has an unique index number,with that index number you can access the element.
  • By index number you can't access whole string ,you have to go character by character.
  • To access whole string you can use built-in function puts()
  • With the help of puts you can print the hole string by just passing the array name with row index.
  • Let access some strings with loops and by using built-in functions

Accessing strings through loops

#include

void main()
{   // this char array has space of storing 3 string
    // with maximum 15 character in  each string.
    char char1[3][15] ={"Computer","Laptop","Cell Phone"};
     int i,j;


    for(i=0;i3;i++)
    {   
        for(j=0;char1[i][j] != '\0';j++) //as we know every string ends with an '\0'
                                     //null character,so we are running this loop until 
                                   //character is '\0' a null character
        {
            printf("%c",char1[i][j]);
        }
        printf("\n");
        //after completing j loop,cursor should go on new line.
    }



}

Output:
Computer
Laptop
Cell Phone
Same Output can occur very easily by using built-in function ,let's see it.

#include<stdio.h>

void main()
{   // this char array has space of storing 3 string
    // with maximum 15 character in  each string.
    char char1[3][15] ={"Computer","Laptop","Cell Phone"};
     int i,j;


    for(i=0;i<3;i++)
    {  //puts() only need row index to print the whole string.
        puts(char1[i]);

    }
}

Output:
Computer
Laptop
Cell Phone

You saw how easy and quick it was to print the whole string by using built-in functions .





Further Concepts: