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];
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];
}
#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",.....,...};
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"};
}
#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.
}
}
#includevoid 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
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]);
}
}
#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
Laptop
Cell Phone
You saw how easy and quick it was to print the whole string by using built-in functions .
Further Concepts:
Further Topics:
- Functions In C
- Why Functions?
- What are Functions?
- how to write our functions?
- What are programmer define functions?
- How to declare function?
- How to define a function?
- What is function Prototype?
- how to declare a function?
- what are Actual Parameters?
- What are Formal Parameters?
- Function Call?
- how to call a Function?
- What are different types of calling function?
- Call by value
- call by reference
- Recursion
- Array and Functions
- How to pass an array to a function?
- Practice programs
People Also Searched:
- What is Character array
- Functions for Characters
- Functions from #include<ctype.h> Header File
- what are isdigit(), islower() , isupper(),etc
- what are various string handling functions
- Functions from #include<string.h> Header File
- what are strcpy(), strcmp(), strncmp(), strcmpi(),etc
- strcmp() VS strncmp() VS strcmpi()
- Input And Output Functions
- How to take strings As Input
- What is gets() functions
- What is puts() functions
- Strings In C
- Concept of strings
- Concept of Initializing One Dimensional Array
- How to take array elements as input from the User
- How to declare 1D Array?
- How to Initialize 1D Array?
- What are Array?
- Need Of Array
- Why Array?
- Concept of Array
0 Comments
Post a Comment