UNIT V STRUCTURES AND UNIONS Introduction – need for structure data type – structure definition – Structure declaration – Structure within a structure Union Programs using structures and Unions – Storage classes, Preprocessor directives.
Array Structure
Single name that contains a collection of data
items of same data type.
Single name that contains a collection of dataitems of different data types.
Individual entries in a array are called elements. Individual entries in a structure are called members.
No Keyword Keyword: struct
Members of an array are stored in sequence of memory locations.
Members of a structure are not stored in sequence of memory locations.
5.1 STRUCTURES
Structure is a collection of variables under the single name, which can be of different data type. In simple words, Structure is a convenient way of grouping several pieces of
related
information together.
A structure is a derived data type, The scope of the name of a structure member is limited to the structure itself and also to any variable declared to be of the structure's type. Variables which are declared inside the structure are called “members of structure”.
Syntax: In general terms, the composition of a structure may be defined as:
struct <tag>
{
member 1;
member 2;
member m;
};
1
GE 6151 COMPUTER PROGRAMMING UNIT_5
where, struct is a keyword, <tag> is a structure name.
For example:
struct student
{
char name [80];
int roll_no;
float marks;
};
Now we need an interface to access the members declared inside the structure, it is called structure variable. we can declare the structure variable in two ways:
i) within the structure definition itself. ii) within the main function.
i]within the structure definition itself.
struct tag
{
member 1;
member 2;
——
—
member m;
} variable 1, variable 2 variable n; //Structure variables
Eg:
struct student
{
char name [80];
int roll_no;
2
GE 6151 COMPUTER PROGRAMMING UNIT_5
float marks;
}s1;
ii]within the main function.
struct tag
{
member 1;
member 2;
——
—
member m;
};
void main()
{
struct tag var1, var2,...,var_n; //structure variable
}
Eg:
struct student
{
char name [80];
int roll_no;
float marks;
};
void main()
{
struct student s1, s2; //declaration of structure variable.
};
3
GE 6151 COMPUTER PROGRAMMING UNIT_5
Note: Structure variable can be any of three types: normal variable, array_variable, pointer_variable.
Initialization of Structure members:
A structure variable, like an array can be initialized in two ways: I. struct student
{
char name [80];
int roll_no;
float marks ;
} s1={“Saravana”,34,469};
II. struct student
{
char name [80];
int roll_no;
float marks ;
} s1;
struct student s1= {“Saravana”,34,469};
[OR] s1.name={“Saravana”}; s1.roll_no=34; s1.marks=500;
It is also possible to define an array of structure, that is an array in which each element is a structure. The procedure is shown in the following example:
struct student
{
char name [80];
int roll_no ;
float marks ;
} st [100];
4
GE 6151 COMPUTER PROGRAMMING UNIT_5
In this declaration st is a 100 element array of structures. It means each element of st represents an individual student record.
Accessing the Structure Members
The members of a structure are usually processed individually, as separate entities. Therefore, we must be able to access the individual structure members. A structure
member can
be accessed by writing: structure_variable.member_name //[normal variable] Eg:
struct student
{
char name [80];
int roll_no ;
float marks ;
}st;
e.g. if we want to get the detail of a member of a structure then we can write as scanf(“%s”,st.name); or scanf(“%d”, &st.roll_no) and so on.
if we want to print the detail of a member of a structure then we can write as
printf(“%s”,st.name); or printf(“%d”, st.roll_no) and so on.
The use of the period operator can be extended to arrays of structure by writing:
array [expression].member_name //[array variable]
Eg:
struct student
{
char name [80];
int roll_no ;
5
GE 6151 COMPUTER PROGRAMMING UNIT_5
float marks ;
}st[10];
e.g. if we want to get the detail of a member of a structure then we can write as scanf(“%s”,st[i].name); or scanf(“%d”, &st[i].roll_no) and so on.
if we want to print the detail of a member of a structure then we can write as
printf(“%s”,st[i].name); or printf(“%d”, st[i].roll_no) and so on.
The use of the period operator can be extended to pointer of structure by writing:
array [expression]>member_name //[array variable]
Eg:
struct student
{
char name [80];
int roll_no ;
float marks ;
}*st;
void main()
{
Struct student s;
st=&s;
e.g. if we want to get the detail of a member of a structure then we can write as scanf(“%s”,st>name); or scanf(“%d”, &st>roll_no) and so on.
if we want to print the detail of a member of a structure then we can write as
printf(“%s”,st>name); or printf(“%d”, st>roll_no) and so on.
6
GE 6151 COMPUTER PROGRAMMING UNIT_5
It is also possible to pass entire structure to and from functions though the way this is done varies from one version of 'C' to another.
PR
No comments:
Post a Comment