ARRAYS AND STRINGS

Contents

Single and Multidimensional Arrays: Array Declaration and Initialization of arrays – Arrays as function arguments. Strings: Initialization and String handling functions. Structure and Union: Definition and Declaration – Nested Structures, Array of Structures, Structure as function arguments, Function that return structure – Union.

ARRAYS

Introduction:

So far we have used only single variable name for storing one data item. If we need to store multiple copies of the same data then it is very difficult for the user. To overcome the difficulty a new data structure is used called arrays.

An array is a linear and homogeneous data structure

An array permits homogeneous data. It means that similar types of elements are stored contiguously in the memory under one variable name.

   An array can be declared of any standard or custom data type.

Example of an Array:

Suppose we have to store the roll numbers of the 100 students the we have to declare 100 variables named as roll1, roll2, roll3, ……. roll100 which is very difficult job. Concept of C programming arrays is introduced in C which gives the capability to store the 100 roll numbers in the contiguous memory which has 100 blocks and which can be accessed by single variable name.

  1. C Programming Arrays is the Collection of Elements
  2. C Programming Arrays is collection of the Elements of the same data
  3. All Elements are stored in the Contiguous memory
  4. All elements in the array are accessed using the subscript variable (index).

Pictorial representation of C Programming Arrays

The above array is declared as int a [5];

a[0] = 4;           a[1] = 5;           a[2] = 33;         a[3] = 13;         a[4] = 1;

In the above figure 4, 5, 33, 13, 1 are actual data items. 0, 1, 2, 3, 4 are index variables.

Index or Subscript Variable:

  1. Individual data items can be accessed by the name of the array and an integer enclosed in square bracket called subscript variable / index
    1. Subscript Variables helps us to identify the item number to be accessed in the contiguous

    What is Contiguous Memory?

    1. When Big Block of memory is reserved or allocated then that memory block is called as Contiguous Memory
    2. Alternate meaning of Contiguous Memory is continuous
    3. Suppose inside memory we have reserved 1000-1200 memory addresses for special purposes then we can say that these 200 blocks are going to reserve contiguous memory.

    Contiguous Memory Allocation

    1. Two registers are used while implementing the contiguous memory These registers are base register and limit register.
    2. When OS is executing a process inside the main memory then content of each register are as
Register Content of register
Base register Starting address of the memory location where process execution is happening
Limit register Total amount of memory in bytes consumed by process

Here diagram 1 represents the contiguous allocation of memory and diagram 2 represents non- contiguous allocation of memory.

  1. When process try to refer a part of the memory then it will firstly refer the base address from base register and then it will refer relative address of memory location with respect to base

How to allocate contiguous memory?

  1. Using static array
  2. Using alloc ( ) / malloc ( ) function to allocate big chunk of memory dynamically.

Array Terminologies:

Size: Number of elements or capacity to store elements in an array. It is always mentioned in square brackets [ ].

Type: Refers to data type. It decides which type of element is stored in the array. It is also instructing the compiler to reserve memory according to the data type.

Base: The address of the first element is a base address. The array name itself stores address of the first element.

Index: The array name is used to refer to the array element. For example num[x], num is array and x is index. The value of x begins from 0.The index value is always an integer value.

Range: Value of index of an array varies from lower bound to upper bound. For example in num[100] the range of index is 0 to 99.

Word: It indicates the space required for an element. In each memory location, computer can

store a data piece. The space occupation varies from machine to machine. If the size of element is more than word (one byte) then it occupies two successive memory locations. The variables of data type int, float, long need more than one byte in memory.

E-COMMERCE-THEORY PRACTICAL 

e – commerce industry

Characteristics of an array :

  1. The declaration int a [5] is nothing but creation of five variables of integer types in memory instead of declaring five variables for five values.
  2. All the elements of an array share the same name and they are distinguished from one another with the help of the element
  3. The element number in an array plays a major role for calling each
  4. Any particular element of an array can be modified separately without disturbing the other
  5. Any element of an array a[ ] can be assigned or equated to another ordinary variable or array variable of its
  6. Array elements are stored in contiguous memory

Array Declaration:

Array has to be declared before using it in C Program. Array is nothing but the collection of elements of similar data types.

Syntax: <data type> array name [size1][size2]….. [sizen];

Syntax Parameter Significance
Data type Data Type of Each Element of the array
Array name Valid variable name
Size Dimensions of the Array

Array declaration requirements

Requirement Explanation
 

Data Type

Data Type specifies the type of the array. We can compute the size required for storing the single cell of array.
 

Valid Identifier

Valid identifier is any valid variable or name given to the array. Using this identifier name array can be accessed.
Size of Array It is maximum size that array can have.

What does Array Declaration tell to Compiler?

  1. Type of the Array
  2. Name of the Array
  3. Number of Dimension
  4. Number of Elements in Each Dimension

Types of Array

  1. Single Dimensional Array / One Dimensional Array
  2. Multi Dimensional Array

Single / One Dimensional Array:

  1. Single or One Dimensional array is used to represent and store data in a linear
  2. Array having only one subscript variable is called One-Dimensional array
  3. It is also called as Single Dimensional Array or Linear Array Single Dimensional Array Declaration and initialization:

Syntax for declaration: <data type> <array name> [size];

Examples for declaration: int iarr[3]; char carr[20]; float farr[3];

Syntax for initialization: <data type> <array name> [size] = {val1, val2, …, valn}; Examples for initialization:

int iarr[3] = {2, 3, 4};

char carr[20] = “program”;

float farr[3] = {12.5, 13.5, 14.5};

Different Methods of Initializing 1-D Array

Whenever we declare an array, we initialize that array directly at compile time.

Initializing 1-D Array is called as compiler time initialization if and only if we assign certain set of values to array element before executing program. i.e. at compilation time.

Here we are learning the different ways of compile time initialization of an array.

Ways of Array Initializing 1-D Array :

  1. Size is Specified Directly
  2. Size is Specified Indirectly

Method 1: Array Size Specified Directly

In this method, we try to specify the Array Size directly. int num [5] = {2,8,7,6,0};

In the above example we have specified the size of array as 5 directly in the initialization statement. Compiler will assign the set of values to particular element of the array.

num[0] = 2;      num[1] = 8;      num[2] = 7;      num[3] = 6;      num[4] = 0;

As at the time of compilation all the elements are at specified position So This initialization scheme is Called as “Compile Time Initialization“.

Graphical Representation :

Method 2: Size Specified Indirectly

In this scheme of compile time Initialization, We do not provide size to an array but instead we provide set of values to the array.

int num[ ] = {2,8,7,6,0};

Explanation:

  1. Compiler Counts the Number Of Elements Written Inside Pair of Braces and Determines the Size of An
  2. After counting the number of elements inside the braces, The size of array is considered as 5 during complete
  3. This type of Initialization Scheme is also Called as “Compile Time Initialization

Example Program

#include < stdio.h > int main ( )
int num [ ] = { 2, 8, 7, 6, 0 } ;
int i ;
for ( i= 0 ; i< 5 ; i ++ )  {
print f ( “\n Array Element num [ %d ] = %d ” , i , num [ i ] ) ; } return 0; }
Output :

Array Element num [ 0 ] = 2
Array Element num [ 1 ] = 8
Array Element num [ 2 ] = 7
Array Element num [ 3 ] = 6
Array Element num [ 4 ] = 0

Accessing Array
1. We all know that array elements are randomly accessed using the subscript variable.
2. Array can be accessed using array-name and subscript variable written inside pair of square brackets [ ].
Consider the below example of an array

In this example we will be accessing array like this

arr [ 5 ] = Sixth Element of Array
whereas elements are assigned to an array using below way
arr[0] = 51; arr [1] = 32; arr [2] = 43; arr [3] = 24; arr [4] = 5; arr [5] =26;
Example Program1: Accessing array #include<stdio.h> #include<conio.h>
void main()
{
int arr [ ] = { 51, 32, 43, 24, 5, 26 } ;
int i ;
for ( i = 0 ; i < = 5 ; i ++ )  {
printf ( “\n Element at arr [ %d ] is %d ” , i , arr [ i ] ) ;
}
getch ( ) ;
}
Output:
Element at arr [ 0 ] is 51
Element at arr [ 1 ] is 32
Element at arr [ 2 ] is 43
Element at arr [ 3 ] is 24
Element at arr [ 4 ] is 5
Element at arr [ 5 ] is 26

How a[i] Works?
We have following array which is declared like int arr[] = { 51,32,43,24,5,26};
As we have elements in an array, so we have track of base address of an array. Below things are important to access an array.

Expression Description Example
arr It returns the base address of an array Consider 2000
*arr It gives zeroth element of an array 51
Expression Description Example
*(arr+0) It also gives zeroth element of an array 51
*(arr+1) It gives first element of an array 32

So whenever we tried accessing array using arr[i] then it returns an element at the location*(arr+ i)

Accessing array a[i] means retrieving element from address (a + i).

Example Program2: Accessing array

#include < stdio.h >

#include < conio.h >
void main ( )
{
int arr [ ] = { 51, 32, 43, 24, 5, 26 } ;
int i ;
for ( i = 0 ; i < = 5 ; i ++ )  {
printf ( ” \n %d %d %d %d “,arr [ i ], * ( i + arr ), * ( arr + i ), i [ arr ] ) ;
}
getch();
}
Output:
51      51       51     51
32      32      32      32
43      43      43      43
24      24      24      24
5        5         5       5
26      26      26      26

Operations with One Dimensional Array

  1. Deletion – Involves deleting specified elements form an
  2. Insertion – Used to insert an element at a specified position in an
  3. Searching – An array element can be The process of seeking specific elements in an array is called searching.
  4. Merging – The elements of two arrays are merged into a single
  5. Sorting – Arranging elements in a specific order either in ascending or in descending

Example Programs :
1. C Program for deletion of an element from the specified location from an Array

#include < stdio.h >
int main (  )  {
int arr [ 30 ],  num , i , loc ;
printf ( “\n Enter no of elements : ” ) ;
scanf ( “% d ” , & num ) ;
//Read elements in an array
printf ( ” \n Enter % d elements : “, num ) ;
for ( i = 0 ; i < num ; i ++ )  {
scanf ( ” % d ” , & arr [ i ] ) ; }
//Read the location
printf ( ” \n Location of the element to be deleted : ” ) ;
scanf ( ” % d “, & loc ) ;
/* loop for the deletion  */
while ( loc  <  num )  {
arr [ loc  –  1 ] =  arr [ loc ] ;
loc ++ ;  }
num — ;  // No of elements reduced by 1
// Print Array
for ( i =  0 ;  i  < num ;  i ++ )
printf ( ” \n  % d ” ,  arr [ i ] ) ;
return  ( 0 ) ;
}
Output:
Enter no of elements :  5
Enter 5 elements :  3  4  1  7  8

Location of the element to be deleted :  3  3  4  7  8

2. C Program to delete duplicate elements from an array

int main ( )  {
int arr [ 20 ] ,  i ,  j ,  k ,  size ;
printf ( ” \n Enter array size :  ” ) ;
scanf ( ” % d ” ,  & size ) ;
printf ( ” \n Accept Numbers :  ” ) ;
for ( i  =  0 ;  i  < size ;  i ++ )
scanf ( ” %d ” ,  & arr [ i ] ) ;
printf ( ” \n Array with Unique list :  ” ) ;
for  ( i  = 0 ;  i  <  size ;  i ++ )  {
for  ( j  =  i  +  1 ;  j  <  size ; )  {
if  ( arr [ j ]  ==  arr [ i ] )  {
for ( k =  j ;  k  <  size ;  k ++ )  {
arr [ k ] =  arr [ k  +  1 ] ;  }
size — ;  }
else
j ++ ;  }
}
for  ( i  =  0 ;  i < size ;  i ++ )  {
printf ( ” % d  ” ,  arr [ i ]) ;  }
return  ( 0 ) ;
}
Output :
Enter array size :  5
Accept Numbers :  1  3  4  5  3
Array with Unique list :  1  3  4  5

3. C Program to insert an element in an array

# include < stdio.h >
int main (  )  {
int arr [ 30 ] , element , num , i , location ;
printf ( ” \n Enter no of elements : ” ) ;
scanf ( ” % d ” , & num ) ;
for (i = 0; i < num; i++) {
scanf ( ” % d ” , & arr [ i ] ) ; }
printf ( ” \n Enter the element to be inserted : ” ) ;
scanf ( ” % d ” , & element ) ;
printf  ( ” \n Enter the location ” ) ;
scanf  ( ” %d ” , & location ) ;
// Create space at the specified location
for  ( i = num ; i > = location ;  i — )  {
arr [ i ] = arr [ i – 1] ; }
num ++ ;
arr [ location – 1 ] = element ;
// Print out the result of insertion
for ( i =  0 ;  i  < num ;  i ++ )
printf ( ” n %d ” , arr [ i ]  ) ;
return ( 0 ) ;
}
Output :
Enter no of elements :  5  1  2  3  4  5
Enter the element to be inserted :  6
Enter the location :  2
1  6  2  3  4  5

2. C Program to find inverse of 3 X 3 Matrix

#include < stdio.h >
void reduction ( float a [ ] [ 6 ] , int size , int pivot , int col )  {
int i , j ;
float factor ;
factor = a [ pivot ] [col ] ;
for ( i = 0 ; i < 2 * size ; i ++ )  {
a [ pivot ] [ i ] / = factor ; }
for ( i =  0 ;  i <  size ; i ++ )
{ if ( i  ! = pivot )  {
factor = a [ i ] [ col ] ;
for ( j = 0 ; j < 2 *  size ;  j ++ )  {
a [ i ] [ j ]  = a [ i ] [ j ]  –  a [ pivot ] [ j ] *  factor ; }  }  }  }
void main ( )  {
float matrix [ 3 ] [ 6 ] ;
int  i , j ;
for  ( i =  0; i < 3; i++) {
for ( j = 0 ; j < 6 ; j ++ )  {
if ( j  ==  i + 3 )  {
matrix [ i ] [ j ] = 1 ; }
else {
matrix [ i ]  [ j ] = 0 ;  }  }  }
printf ( ” \n Enter  a  3  X  3  Matrix  : ” ) ;
for ( i  =  0 ; i < 3 ;  i ++ )  {
for  ( j  =  0 ;  j  <  3 ;  j ++ )  {
scanf ( ” %f ” ,  & matrix [ i ] [ j ] ) ;  }  }
for ( i  =  0 ;  i <  3 ;  i ++ )  {
reduction ( matrix ,  3  ,   i ,  i ) ;  }
printf( “\ nInverse  Matrix ” ) ;
for ( i  =  0 ;  i  <  3 ;  i ++ )  {
printf ( ” \n ” ) ;
for  ( j  =  0 ;  j  <  3 ;  j ++ )  {
printf ( ” % 8.3f ” ,  matrix  [ i ] [ j + 3 ] ) ;  }  }  }
Output :
Enter  a  3  X  3  Matrix
1   3   1
1   1   2
2   3   4

Inverse Matrix

2.000          9.000         -5.000
0.000        -2.000          1.000
-1.000      -3.000           2.000

 

3. C Program to Multiply two 3 X 3 Matrices

#include < stdio.h >
int main ( ) {
int a [ 10 ] [ 10 ] , b [ 10 ] [ 10 ] , c [ 10 ] [ 10 ] , i , j , k ;
int sum = 0 ;
printf ( ” \n Enter First Matrix : ” );
for ( i = 0 ; i < 3 ; i ++ ) {
for ( j = 0 ; j < 3 ; j ++) {
scanf ( ” %d ” , & a [ i ] [ j ] ) ; } }
printf ( ” \n Enter Second Matrix : ” ) ;
for ( i = 0 ; i < 3 ; i ++ ) {
for ( j = 0 ; j < 3 ; j ++ ) {
scanf ( ” %d ” , & b [ i ] [ j ] ) ; } }
printf ( ” The First Matrix is : \n ” ) ;
for ( i = 0 ; i < 3 ; i ++ ) {
for (j = 0; j < 3; j++) {
printf ( ” %d ” , a [ i ] [ j ] ) ; }
printf ( ” \n ” ) ; }
printf ( ” The Second Matrix is : \n ” ) ;
for ( i = 0 ; i < 3 ; i ++ ) {
for ( j = 0 ; j < 3 ; j ++ ) {
printf ( ” %d ” , b [ i ] [ j ] ) ; }
printf ( ” \n ” ) ; }
// Multiplication Logic
for ( i = 0 ; i <= 2 ; i ++ ) {
for ( j = 0 ; j <= 2 ; j ++ ) {
sum = 0 ;

for ( k = 0 ; k <= 2 ; k ++ ) {
sum = sum + a [ i ] [ k ] * b [ k ] [ j ] ; }
c [ i ] [ j ] = sum ; } }
printf ( “\n Multiplication Of Two Matrices : \n ” ) ;
for ( i = 0 ; i < 3 ; i ++ ) {
for ( j = 0 ; j < 3 ; j ++ ) {
printf ( ” %d ” , c [ i ] [ j ] ) ; }
printf(“\n”); }
return ( 0) ;
}
Output :
Enter First Matrix :

1      1     1
1      1     1
1      1     1
Enter Second Matrix :
2      2      2
2      2      2
2      2      2
The First Matrix is :
1      1      1
1      1     1
1      1    1
The Second Matrix is :
2       2     2
2       2     2
2       2     2
Multiplication Of Two Matrices :

6      6     6
6      6     6
6      6     6

Multiplication is possible if and only if
i.   No. of Columns of Matrix 1 = No of Columns of Matrix 2
ii.  Resultant Matrix will be of Dimension – c [ No. of Rows of Mat 1 ] [ No. of Columns of Mat 2 ]

STRUCTURES

Arrays are used for storing a group of SIMILAR data items. In order to store a group of data items, we need structures. Structure is a constructed data type for packing different types of data that are logically related. The structure is analogous to the “record” of a database. Structures are used for organizing complex data in a simple and meaningful way.

Example for structures :

Student : regno, student_name, age, address

Book :  bookid, bookname, author, price, edition, publisher, year

Employee : employeeid, employee_name, age, sex, dateofbirth, basicpay

Customer : custid, cust_name, cust_address, cust_phone

 

Structure Definition

Structures are defined first and then it is used for declaring structure variables. Let us see how to define a structure using simple example given below:

struct book

{

int bookid ;

char bookname [ 20 ] ;  char author [ 20 ] ; float price ;

int year ;

int pages ;

char publisher [ 25 ] ;

} ;

The keyword “struct” is used for declaring a structure. In this example, book is the name of the structure or the structure tag that is defined by the struct keyword. The book structure has six fields and they are known as structure elements or structure members. Remember each structure member may be of a different data type. The structure tag name or the structure name can be used to declare variables of the structure data type.

The syntax for structure definition is given below :

struct tagname

{

Data_type member 1 ;

Data_type member 2 ;

…………….

……………

} ;

 

Note :

  1. To mark the completion of the template, semicolon is used at the end of the
  2. Each structure member is declared in a separate

 

Declaring Structure Variables

First, the structure format is defined.   Then the variables can be declared of that structure type. A structure can be declared in the same way as the variables are declared. There are two ways for declaring a structure variable.

  • Declaration of structure variable at the time of defining the structure ( i.e structure definition and structure variable declaration are combined )

struct book

{

int bookid ;

char bookname [ 20 ] ;

char author [ 20 ] ;

float price ;

int year ;

int pages ;

char publisher [ 25 ] ;

}  b1 , b2 , b3 ;

The b1 , b2 , and b3 are structure variables of type struct book.

  • Declaration of structure variable after defining the structure

struct book

{

int bookid ;

char bookname [ 20 ] ;

char author [ 20 ] ;

float price ;

int year ;

int pages ;

char publisher [ 25 ] ;

} ;

struct book b1 , b2 , b3 ;

NOTE :

  • Structure tag name is optional.

E.g.

struct

{

int bookid ;

char bookname [ 20 ] ;

char author [ 20 ] ;

float price ;

int year ;

int pages ;

char publisher [ 25 ] ;

} b1,  b2,  b3 ;

Declaration of structure variable at a later time is not possible with this type of declaration. It is a drawback in this method.  So the second method can be preferred.

  • Structure members are not variables. They don‟t occupy memory until they are associated with a structure variable.

Accessing Structure Members

There are many ways for storing values into structure variables. The members of a structure can be accessed using a “dot operator” or “period operator”.

E.g. b1.author  -> b1 is a structure variable and author is a structure member.

Syntax

STRUCTURE_Variable.STRUCTURE_Members

The different ways for storing values into structure variable is given below :

Method 1: Using Simple Assignment Statement

b1.pages = 786 ;

b1.price = 786.50 ;

Method 2 : Using strcpy function

strcpy ( b1.title,  ‚Programming in C ‛ ) ;

strcpy ( b1.author,  ‚ John ‛ ) ;

Method 3 : Using scanf function

scanf ( ‚ %s  \n ‛ ,  b1 .title ) ;

scanf ( ‚ %d \n ‛ , &b1.pages ) ;

Example

#include < stdio.h >

#include < conio.h >

struct book

{

int bookid ;

char bookname [ 20 ] ;

char author [ 20 ] ;

float price ;

int year ;

int pages ;

char publisher [ 25 ] ;

} ;

struct book b1, b2, b3; main()

{

struct book b1 ;

clrscr ( ) ;

printf ( “Enter the Book Id : ” ) ;

scanf ( ” %d ” , &b1.bookid ) ;

printf ( ” Enter the Book Name :  ” ) ;

scanf ( ” %s ” ,  b1.bookname ) ;

printf ( ” Enter the Author Name :  ” ) ;

scanf ( ” %s ” , b1.author ) ;

printf ( ” Enter the Price :  ” ) ;

scanf ( ” %f ” , & b1.price ) ;

printf ( ” Enter the Year :  ” ) ;

scanf ( ” %d ” , &b1.year ) ;

printf ( ” Enter the Total No. of Pages :  ” ) ;

scanf ( ” %d ” , &b1.pages ) ;

printf ( ” Enter the Publisher Name : ” ) ;

scanf ( ” %s ” , b1.publisher ) ;

printf ( ” %d  %s %d  %f  %d  %d  %s ” ,  b1.bookid ,  b1.bookname ,  b1.author ,  b1.price ,  b1.year ,  b1.pages ,  b1.publisher ) ;

getch ( ) ;

}

Output 

Enter the Book Id : 786

Enter the Book Name : Programming

Enter the Author Name : John

Enter the Price : 123.50

Enter the Year : 2015

Enter the Total No. of Pages : 649

Enter the Publisher Name : Tata McGraw

786 Programming      2118 123.500000      2015 649 Tata

 

Structure Initialization

Like variables, structures can also be initialized at the compile time.

Example

main ( )

{

struct

{

int rollno ;

int attendance ;

}

s1 = { 786 , 98 } ;

}

The above example assigns 786 to the rollno and 98 to the attendance.

Structure variable can be initialized outside the function also.

Example

main ( )

{

struct student

{

int rollno ;

int attendance ;

} ;

}

Note :

struct student s1 = { 786 , 98 } ;

struct student s2 = { 123, 97 } ;

Individual structure members cannot be initialized within the template.

Initialization is possible only with the declaration of structure members.

 

Nested Structures or Structures within Structures 

Structures can also be nested. i.e A structure can be defined inside another structure.

Example

struct employee

{

int empid ;

char empname [ 20 ] ;

int basicpay ;

int da ;

int hra ;

int cca ;

}

e1 ;

In the above structure, salary details can be grouped together and defined as a separate structure.

Example 

struct employee

{

int empid ;

char empname [ 20 ] ;

struct

{

int basicpay ;

int da ;

int hra ;

int cca ;

} salary ;

} e1 ;

The structure employee contains a member named salary which itself is another structure that contains four structure members.   The members inside salary structure can be referred as below :

e1.salary.basicpay

e1.salary.da ;

e1.salary.hra ;

e1.salary.cca ;

However, the inner structure member cannot be accessed without the inner structure variable.

Example 

e1.basicpay

e1.da e1.hra

e1.cca

are invalid statements

Moreover, when the inner structure variable is used, it must refer to its inner structure member. If it doesn‟t refer to the inner structure member then it will be considered as an error.

Example

e1.salary   ( salary is not referring to any inner structure member. Hence it is wrong )

Note: C permits 15 levels of nesting and C99 permits 63 levels of nesting.

 

Array of Structures

A Structure variable can hold information of one particular record. For example, single record of student or employee. Suppose, if multiple records are to be maintained, it is impractical to create multiple structure variables. It is like the relationship between a variable and an array. Why do we go for an array? Because we don‟t want to declare multiple variables and it is practically impossible. Assume that you want to store 1000 values. Do you declare 1000 variables like a1, a2, a3…. Upto a1000? Is it easy to maintain such code ? Is it a good coding? No. It is not. Therefore, we go for Arrays. With a single name, with a single variable, we can store 1000 values. Similarly, to store 1000 records, we cannot declare 1000 structure variables. But we need “Array of Structures”.

An array of structure is a group of structure elements under the same structure variables.

struct student s1 [ 1000 ] ;

The above code creates 1000 elements of structure type student. Each element will be structure data type called student. The values can be stored into the array of structures as follows:

s1 [ 0 ] .student _ age = 19 ;

Example

#include < stdio.h >

#include < conio.h >

struct book

{

int bookid ;

char bookname [ 20 ] ;

char author [ 20 ] ;

} ;

Struct b1 [ 5 ] ;

main ( )

{

int i ;

clrscr ( ) ;

for ( i = 0 ; i < 5 ; i ++ )

{

printf ( ” Enter the Book Id :  ” ) ;

scanf( ” %d ” , &b1 [ i ] .bookid ) ;

printf ( ” Enter the Book Name : ” ) ;

scanf( ” %s ” , b1 [ i ] .bookname ) ;

printf ( ” Enter the Author Name :  ” ) ;

scanf( ” %s ” ,  b1 [ i ].author ) ;

}

for ( i = 0 ; i < 5 ; i ++ )

{

printf ( ” %d  \t  %s  \t  %s  \n ” , b1 [ i ] .bookid , b1 [ i ] .bookname , b1 [ i ] .author ) ;

}

getch ( ) ;

}

Output :

Enter the Book Id : 786

Enter the Book Name : Programming Enter the Author Name: Dennis Ritchie Enter the Book Id: 101

Enter the Book Name : Java Complete Reference Enter the Author Name:   Herbert Schildt Enter the Book Id: 008

Enter the Book Name : Computer Graphics

Enter the Author Name: Hearn and Baker

786   Programming   Dennis Ritchie

101   Java Complete Reference   Herbert Schildt

008    Computer Graphics   Hearn and Baker

34 thoughts on “ARRAYS AND STRINGS

Leave a Reply

Your email address will not be published. Required fields are marked *