Home » Learn C++ » Structure Variables as Arrays

Structure Variables as Arrays

Structure Variables as Arrays:

A structure variable may also be declared as an array. This is done to manage a large number of records. Each variable of the array represents a complete record.
For example, in the following example the structure variable arts are of array type. It contains 10 elements. Each element can store one record.

struct result
{
char s_name [15];
int sub [4] ;
int total;

};
result arts [10];

There are three members of the above structure. Each member is accessed by one element of the arts array. The indexed variables are used to access these records. The index of first element or record is arts [0]. Similarly, the last record is arts [9].

Initialization of Array of Structure:

Like an array, the data into array of structure variables can also be assigned at the time of its declaration.
For example, in the following example the structure variable “rec” has three elements. The data of fields of each record is enclosed in braces and is separated by commas.

struct marks
{
char code [10];
char name[15];
float marks;
}
marks rec [3] = {{“man-1”, “James ”, 45. 9},
{“acc-l”, “John”, 45.9},
{“fac-l”, “William”, 55.8}};

Other Topics

comment closed

Get Adobe Flash playerPlugin by wpburn.com wordpress themes
Copyright © 2010 Information Village. All rights reserved.