Structures
A Structure is defined as a data type to represent different types of data with a single name. The data items in a structure are called members of the structure.
The data items in a structure are called members of the structure.
Defining a structure:
A structure definition contains a keyword ‘struct’ and a user defined tag-field followed by the members of the structure within braces. General form:
Struct tag-field
{
datatype member 1;
datatype member 2;
----------------------
-----------------------
datatype member n;
};
Where
struct – keyword to define structure
tag-field – valid C++ name
datatype - valid datatype such as int, float etc.
Rules:
Eg:
Struct student
{
int number;
int age;
char sex;
};
This defines a structure with three members namely number, age and sex of different data types. The name of the structure is student.
The members of the structure definition are not variables. So they do not occupy any memory space.
Structure declaration:
Structure declaration means defining variables to the already defined structure
We can define variables in two ways. They are
General forms:
a) Struct tag-field
{
datatype member1;
datatype member2;
---------------------
-----------------------
datatype membern;
} variable1, variable 2, …..variablen;
b) tag-field variable1, variable2…..variablen;
where
variable1,variable2,…..variablen are valid C++ variable names each having n-members
Eg:
Struct student
{
int number;
int age;
char sex;
}student1, student2, student3;
The following declarations are also valid:
a) Struct student
{
int number;
int age;
char sex;
};student student1, student2, student3;
b) Struct
{
int number;
int age;
char sex;
} student1, student2, student3;
Giving values to structure members:
Dot operator or member operator “ . “ is used to give data to the structure variables individual members. General form:
Structure_variable.membername
The variable name with a period and the member name is used like any ordinary variable.
| Name* : |
|||||
| Email* : |
|||||
| Country* : |
|||||
| Phone* : |
|||||
| Subject* : |
|||||
| Upload Homework : Upload another homework (upto 5 uploads max.)
|
|||||
| Due Date |
Time |
AM/PM |
Timezone |
||
| Instructions |
|||||
|
|||||