Wednesday 13 February 2013

Two Dimensional Character Arrays in C Programming Language



Two-d character arrays are typically used to create an array of strings. Manipulating a character two-d array is however, 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] = {
"This is first",
"This is second",
"This is third",
"This is forth"
};

main()
{
int num;
printf("\nEnter a semester number");
scanf("%d", &num);
fflush(stdin);
if(num>=1 && num<=6)
{
num--;
printf("Your book for thi semester is %s", books[num]);
}
else
printf("\nWrong semester !");
}

Consider the need to store the marks of the students belonging to a batch, in various subjects. The list of student names are stored in a two dimensional character array called student. The list of subjects is also stored in a two dimensional array called subject. The details of the marks of each student in each subject are going to be stored in a two dimensional integer array called marks.

0 comments:

Post a Comment

Powered by Blogger.