In the last chapter we saw several examples of 2-dimensional integer arrays. Let’s now look at a similar entity, but one dealing with characters. Our example program asks you to type your name. When you do so, it checks your name against a master list to see if you are worthy of entry to the palace. Here’s the program...
#define FOUND 1
#define NOTFOUND 0
main( )
{
char masterlist[6][10] = {
"akshay",
"parag",
"raman",
"srinivas",
"gopal",
"rajesh"
} ;
int i, flag, a ;
char yourname[10] ;
#define NOTFOUND 0
main( )
{
char masterlist[6][10] = {
"akshay",
"parag",
"raman",
"srinivas",
"gopal",
"rajesh"
} ;
int i, flag, a ;
char yourname[10] ;
printf ( "\nEnter your name " ) ;
scanf ( "%s", yourname ) ;
flag = NOTFOUND ;
for ( i = 0 ; i <= 5 ; i++ )
{
a = strcmp ( &masterlist[i][0], yourname ) ;
if ( a == 0 )
{
printf ( "Welcome, you can enter the palace" ) ;
flag = FOUND ;
break ;
}
}
if ( flag == NOTFOUND )
printf ( "Sorry, you are a trespasser" ) ;
}
scanf ( "%s", yourname ) ;
flag = NOTFOUND ;
for ( i = 0 ; i <= 5 ; i++ )
{
a = strcmp ( &masterlist[i][0], yourname ) ;
if ( a == 0 )
{
printf ( "Welcome, you can enter the palace" ) ;
flag = FOUND ;
break ;
}
}
if ( flag == NOTFOUND )
printf ( "Sorry, you are a trespasser" ) ;
}
And here is the output for two sample runs of this program...
Enter your name dinesh
Sorry, you are a trespasser
Enter your name raman
Welcome, you can enter the palace
Notice how the two-dimensional character array has been initialized. The order of the subscripts in the array declaration is important. The first subscript gives the number of names in the array, while the second subscript gives the length of each item in the array.
Instead of initializing names, had these names been supplied from the keyboard, the program segment would have looked like this...
for ( i = 0 ; i <= 5 ; i++ )
scanf ( "%s", &masterlist[i][0] ) ;
scanf ( "%s", &masterlist[i][0] ) ;
While comparing the strings through strcmp( ), note that the addresses of the strings are being passed to strcmp( ). As seen in the last section, if the two strings match, strcmp( ) would return a value 0, otherwise it would return a non-zero value.
The variable flag is used to keep a record of whether the control did reach inside the if or not. To begin with, we set flag to NOTFOUND. Later through the loop if the names match, flag is set to FOUND. When the control reaches beyond the for loop, if flag is still set to NOTFOUND, it means none of the names in the masterlist[ ][ ] matched with the one supplied from the keyboard.
The names would be stored in the memory as shown in Figure. Note that each string ends with a ‘\0’. The arrangement as you can appreciate is similar to that of a two-dimensional numeric array.
The variable flag is used to keep a record of whether the control did reach inside the if or not. To begin with, we set flag to NOTFOUND. Later through the loop if the names match, flag is set to FOUND. When the control reaches beyond the for loop, if flag is still set to NOTFOUND, it means none of the names in the masterlist[ ][ ] matched with the one supplied from the keyboard.
The names would be stored in the memory as shown in Figure. Note that each string ends with a ‘\0’. The arrangement as you can appreciate is similar to that of a two-dimensional numeric array.
Here, 65454, 65464, 65474, etc. are the base addresses of successive names. As seen from the above pattern some of the names do not occupy all the bytes reserved for them. For example, even though 10 bytes are reserved for storing the name “akshay”, it occupies only 7 bytes. Thus, 3 bytes go waste. Similarly, for each name there is some amount of wastage. In fact, more the number of names, more would be the wastage. Can this not be avoided? Yes, it can be... by using what is called an ‘array of pointers’, which is our next topic of discussion
No comments:
Post a Comment