Unit - 1 Array - Structure
Unit - 1 Array - Structure
dataTypearrayName[rowSize][columnSize];
int matrix[2][3] = {
{1, 2, 3},
{4, 5, 6}
};
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.
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:
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.
Program to search for a string in the string array
#include <stdio.h>
#include <string.h>
intmain(){
char name[5][10],
item[10]; // declaring the string array and the character
// array that will contain the string to be matched
inti, 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
-- Defining a structure:
Unlike arrays, structures must be defined first for their format that may
be used later to declare structure variables. Consider the process of
structure definition and the creation of structure variable. Consider a
book database consisting of book_name, author, number of pages and
price. We can define a structure to hold this information as follows:
struct book_bank
{
char title[20];
char author[15];
int pages;
float price;
};
The keyword struct declares a structure to hold the details of four data
fields, namely title, author, pages and price. These fields are structure
elements or members. Each member may belong to a different type of
data. book_bank is the name of the structure and is called the structure
tag. The tag name may be used subsequently to declare variables that
have the tag’s structure.
-- Arrays vs Structures:
Both the arrays and structures are classified as structured data types as
they provide a mechanism that enable us to access and manipulate data
in a relatively easy manner. But they differ in a number of ways.
E.g.:
struct book_bank
{
char title[20];
char author[15];
int pages;
float price;
};
struct book_bank book1, book2, book3;
Here, on above statement the book1, book2 and book3 are called
objects of the book_bank structure. So the all the structure tags i.e.
structure variables are call by using these object names by using
member operator .(dot).
-- Array of Structures:
E.g.:
struct student
{
int rollno, s1,s2,s3,total;
float per;
};
void main( )
{
struct student s1[100];
}
This declares the student as an array of 100 elements s1[0], s1[1] and
so on..