Introduction:
Structure is a function which stores the block of memory. However, arrays also stores the memory, but they have limitation that they contains same type of data(i.e, only integer type functions or character type etc...).
Some times we require a data structure that can store data items of mixed data type. Certain items are made up of different variables and different members of different data types(i.e, all kinds of data types-integers, characters, float etc...).For example, a bank accountant contains his name, Id, and his date of joining etc... These three were of different kinds of data types. Name is defined by character data type, Id is defined by long int data type, and date of joining is defined by integer data type.
The collective information about an accountant is known as a "structure". precisely, a structure can be defined as "a group of data types of different members."
Here, Name, id, and date of joining are the members of a structure. Each member is a variable that can be referred to through the name of the structure. Variable form a group under a single name for ease of using and handling of same.
Variable
|
Member
|
Data type
|
b1
|
name, id, DOJ
|
char, long int, int
|
b2
|
name, id, DOJ
|
char, long int, int
|
For example, consider a bank accountant structure. Let us assume that the structure defined by a name "Bank"[Don't get confused by the name of the structure and by the name of bank accountant. They both differ each other. Since structure contains all the details of the bank accountant, so bank accountant name is one kind of member of the structure and that structure has a name, defined by the user]. Suppose if there are three banks, then we need to declare three names to identify separately, since all the three banks contains names of the accountant, id etc...Assume Bank1 is declared as b1; Bank2 as b2; Bank3 as b3. Therefore b1, b2, b3 are known as variables of the stucture "Bank". The number of variables and members depends on the user's declaration.
Defining a structure with its keyword:
A structure is defined by a keyword struct. Strcut is followed by the name of the structure and the body of the structure which is enclosed with in curly braces. Whole body contains the declaration of its members. And each member must and should contain the user defined data type(i.e; int, char, float,....). Defining a structure ends by a semicolon.
Structure is declared by the below given syntax:
struct <name>
{
data type member1;
data type member2;
data type member3;
.
.
.
};
For example consider the following structure's definition:
struct Bank
{
char e_name[10];
long int Id;
int DOJ;
};
Here, struct is a keyword(obviously it is compulsory for each and every structure to define). Bank is an identifier or else it can be known as structure's tag name, and also known as user-defined type, since it is given by the user(programmer). The body of the structure contains three members, they are of:
- e_name to store the name of the employer since name contains characters, it is declared by 'char' data type.
- Id to store the id of an employer, since it has all the integer values with long number of digits, so it is declared by 'long int' data type.
- DOJ to store the date of joining, since it has only integer values, so it is declared by 'int' data type.
A point to be noted: The same member names can occur in different structures.
Declaring a structure variable:
If a structure is declared, then we can declare variables and members to store the information. variables are specially known to store the information of particular structure type. Simply we can
say that a file does a work to store all the details provide by the user. In the same way the variable does to store the information(i.e; members). When a structure definition is placed in-front of the program code(i.e; global area), it will be visible to all the functions in the program. In the case, we can declare variables of type structure in global area or with in any function as local variables.
Declaring a variable of type Bank:
struct Bank b1;
This statement can be used in any function.
Once a variable is declared, memory equivalent to total of memory required by individual members of structure Bank is allocated to b1.
Two points to be noted:
1. If we want structure variables to be local to a function, we have to define structure with in that function. In that case, structure becomes local to that function and would not be accessible to other functions.
2. If we give this definition at global place, then the structure variables become global and would be accessible to all functions which is again not recommended.
We can declare a variable in two methods:
1. struct Bank
{
char e_name[20];
long int Id;
int DOJ;
}b1,b2,b3;
2. struct Bank
{
char e_name[20];
long int Id;
int DOJ;
};
void main()
struct Bank b1;
struct Bank b2;
struct Bank b3;
A point to be noted: Tag name is compulsory in the 2nd method. Variable should be declared with its keyword struct and its <tag name>.
Here, struct is constant, and tag name is optional to the user.
Diagrammatic representation of allocating memory to the structure variable b1:
A point to be noted: Structure variables will allocate members in continuous memory locations.
Therefore b1 reserves the sum of the memory bytes declared by the individual members(i.e, 20+4+2=26 bytes).
Declaring values to structure members:
Between structure variable name and member name a dot operator is used to access a structure.
The syntax is given below:
structure_variable_name.member_name
For example consider the below given statements:
A sample program code based on the functions which we have been discussed till now:
simple C program structure to input and output the data
#include<conio.h>
#include<stdio.h>
struct Bank
{
char e_name[20];
long int Id;
int DOJ;
};
void main()
{
struct Bank b1;
printf("enter the details of an employer");
printf("\n enter the name of the employer:");
gets(b1.e_name);
printf("enter the Id:);
scanf("%ld",&b1.Id);
printf("enter date of joining:");
scanf("%d",&b1.DOJ);
printf("\n employer's details");
printf("\n Name of an employer is");
puts(b1.e_name);
printf("\n Id of an employer is %ld",b1.Id);
printf("\n Date of joining of an employer is %d",b1.DOJ);
getch();
}
- Suppose if we want input all the details of an employer who is from Bank1. Therefore declaring values will be like: gets(b1.e_name); scanf("%d",&b1.Id); scanf("%d",&b1.DOJ);
- If we want to print the values then the statements would be like: puts(b1.e_name); printf("%d",b1.Id); printf("%d",b1.DOJ);
A sample program code based on the functions which we have been discussed till now:
simple C program structure to input and output the data
#include<conio.h>
#include<stdio.h>
struct Bank
{
char e_name[20];
long int Id;
int DOJ;
};
void main()
{
struct Bank b1;
printf("enter the details of an employer");
printf("\n enter the name of the employer:");
gets(b1.e_name);
printf("enter the Id:);
scanf("%ld",&b1.Id);
printf("enter date of joining:");
scanf("%d",&b1.DOJ);
printf("\n employer's details");
printf("\n Name of an employer is");
puts(b1.e_name);
printf("\n Id of an employer is %ld",b1.Id);
printf("\n Date of joining of an employer is %d",b1.DOJ);
getch();
}
Output:
enter the details of an employer
enter the name of an employer: Raj
enter the Id: 12100
enter date of joining: 2
employer's details
Name of an employer is Raj
Id of an employer is 12100
Date of joining of an employer is 2
From site : http://ctechrockz.blogspot.com/enter the details of an employer
enter the name of an employer: Raj
enter the Id: 12100
enter date of joining: 2
employer's details
Name of an employer is Raj
Id of an employer is 12100
Date of joining of an employer is 2
Initializing a structure:
Like any other data type we can initialize structures when we declare variables of structure. We can provide initialization values separated by comma with in pair of curly braces. The order of values must match with the order of members declared in the structure definition.
A point to be noted: Data type of the member must be declared in the structure only. And the structure declaration is compulsory.
For example,
struct Bank b1={"Raj",12100, 2};
The initialization of each and every member is as following:
e_name field of name is initialized to "Raj",
Id is initialized to 12100,
DOJ is initialized to 2.
Nested Structure:
The structure inside a structure is known as "nested structure". Simply we can say that nested structure is known when structure acts as a member of another structure. This system can be used to create complex data type structures.
For example, we are required to store information about student such as the name, roll_no. , marks, and date of birth. All of these members act as individual elements except date of birth which is made up of three members, via. day, month, and year. So we can define structure date that itself act as a member of structure student as below.
struct DOB
{
int d;
int m;
int y;
};
struct student
{
char name[10];
int rank;
int marks;
struct DOB dob;
};
Here is an another way to access the structure DOB inside the structure student. It exists with in the scope of the structure student.
struct student
{
char name[10];
int rank;
int marks;
struct DOB
For example, we are required to store information about student such as the name, roll_no. , marks, and date of birth. All of these members act as individual elements except date of birth which is made up of three members, via. day, month, and year. So we can define structure date that itself act as a member of structure student as below.
struct DOB
{
int d;
int m;
int y;
};
struct student
{
char name[10];
int rank;
int marks;
struct DOB dob;
};
Here is an another way to access the structure DOB inside the structure student. It exists with in the scope of the structure student.
struct student
{
char name[10];
int rank;
int marks;
struct DOB
{
int d;
int m;
int y;
}dob;
}s1;
The members of nested structures are accessed from outermost variable to innermost variable with the help of dot (.) operators.
Also here is another way to define the variable of structure student. Directly we can define as given above, orelse we can use a statement such as struct student s1;
}s1;
The members of nested structures are accessed from outermost variable to innermost variable with the help of dot (.) operators.
Also here is another way to define the variable of structure student. Directly we can define as given above, orelse we can use a statement such as struct student s1;
struct student
{
char name[10];
int rank;
int marks;
struct DOB
{
char name[10];
int rank;
int marks;
struct DOB
{
int d;
int m;
int y;
}dob;
};
void main()
struct student s1;
Therefore the following statements are illustrated to initialize the member of structure DOB:
s1.dob.d=24;
s1.dob.m=12;
s1.dob.y=1994;
And if you are willing to initialize only for student members, then the statement would be like:
s1.name="Raj";
We can use more levels of nested structure and access the members of nested structure in the same way from outermost variable to innermost variable names with the help of dot operator.
The following code initializes variable s1 of type student:
struct student s1={"Raj", 32, 680, {24, 12, 1994}};
};
void main()
struct student s1;
Therefore the following statements are illustrated to initialize the member of structure DOB:
s1.dob.d=24;
s1.dob.m=12;
s1.dob.y=1994;
And if you are willing to initialize only for student members, then the statement would be like:
s1.name="Raj";
We can use more levels of nested structure and access the members of nested structure in the same way from outermost variable to innermost variable names with the help of dot operator.
Initialization of Nested Structure
If a member of structure is itself a structure, then initialization values of that structure is enclosed with in curly braces.The following code initializes variable s1 of type student:
struct student s1={"Raj", 32, 680, {24, 12, 1994}};
Array of structures:
In real life, we need to work on number of files like storing the data of accountants, students, employee etc... In that case, we need to create number of variables of type employer, student etc... Here is an easy way to access these variables as an array.
For example, we can create 20 elements array of type employer as below:
struct employer e[20];
Here, 20 contiguous memoryy blocks are reserved to store information for 20 employee. Each block is divided into a set of members declared in the structure. We can work on 20 employee referring to their variable e followed by an index, by a dot operator, and ending with structure member like:
puts(e[i].name);
printf("%ld", e[i].Id);
scanf("%d" , &e[i].DOJ);
We can also initialize the array structure directly.
struct Bank b[5]={{"Raj", 12100, 2},
{"Ravi", 12101, 3},
{"Rahul", 12102, 4},
};
We can also initialize the array structure directly.
struct Bank b[5]={{"Raj", 12100, 2},
{"Ravi", 12101, 3},
{"Rahul", 12102, 4},
};
No comments:
Post a Comment