Structure is user defined data type which is used to store heterogeneous data items under unique name. Keyword 'struct' is used to declare structure.
Each variable in structure is known as a structure member. A structure can contain any valid data types like int, char, float, array or even other structures.
Syntax:
Struct <structure name>
{
<data type 1> <element 1>;
<data type 2> <element 2);
- - - -
- - - - -
<data type n> <element n>;
}<structure Variable>;
Example:
Struct sample
{
int num;
float var;
char ch;
};
Instance of structures:
Instances of structure can be created in two ways
1) Struct sample
{
int num[10];
float var;
}obj;
2) Struct sample
{
int num[10];
float var;
};
Struct sample obj;
Initializing and accessing a Structure:
Structure members can be initialized when you declare a variable of your structure:
Struct sample
{
char name[10];
float x;
int num;
};
Struct sample var={“Program”,1.4,20};
The above declaration will create a struct object called var with an name equal to “Program”, x equal to 1.4, and num equal to 20.
Structure members can be accessed using member operator '.' . It is also called as 'dot operator' or 'period operator'.
/* Program to Explain about a structure.
Author : http://ccppcoding.blogspot.com */
#include <stdio.h>
struct example
{
char EmpName[10];
int id;
};
void main()
{
Struct sample obj;
obj.EmpName=”King”;
obj.id=2100;
printf("\n\n Employee Name : %s",obj.EmpName);
printf("\n\n ID : %d",obj.id);
}
Structures within structures(Nested Structures):
Structures can also be used inside another structure. It is also called as “nesting of structures”.
Example:
Struct company
{
char Name[10];
char address[20];
int count;
struct employee
{
char EmpName[10];
int EmpId;
}empobj;
}compobj;
Inner structure member variables can be accessed as:
compobj.empobj.EmpName;
compobj.empobj.EmpId;
Each variable in structure is known as a structure member. A structure can contain any valid data types like int, char, float, array or even other structures.
Syntax:
Struct <structure name>
{
<data type 1> <element 1>;
<data type 2> <element 2);
- - - -
- - - - -
<data type n> <element n>;
}<structure Variable>;
Example:
Struct sample
{
int num;
float var;
char ch;
};
Instance of structures:
Instances of structure can be created in two ways
1) Struct sample
{
int num[10];
float var;
}obj;
2) Struct sample
{
int num[10];
float var;
};
Struct sample obj;
Initializing and accessing a Structure:
Structure members can be initialized when you declare a variable of your structure:
Struct sample
{
char name[10];
float x;
int num;
};
Struct sample var={“Program”,1.4,20};
The above declaration will create a struct object called var with an name equal to “Program”, x equal to 1.4, and num equal to 20.
Structure members can be accessed using member operator '.' . It is also called as 'dot operator' or 'period operator'.
/* Program to Explain about a structure.
Author : http://ccppcoding.blogspot.com */
#include <stdio.h>
struct example
{
char EmpName[10];
int id;
};
void main()
{
Struct sample obj;
obj.EmpName=”King”;
obj.id=2100;
printf("\n\n Employee Name : %s",obj.EmpName);
printf("\n\n ID : %d",obj.id);
}
Structures within structures(Nested Structures):
Structures can also be used inside another structure. It is also called as “nesting of structures”.
Example:
Struct company
{
char Name[10];
char address[20];
int count;
struct employee
{
char EmpName[10];
int EmpId;
}empobj;
}compobj;
Inner structure member variables can be accessed as:
compobj.empobj.EmpName;
compobj.empobj.EmpId;
No comments:
Post a Comment