- Every Structure member takes different memory space.
- The variable are called member of the structure.
- The Structure are called user defined data type.
Syntax of Structure
struct struct_name
{
data_type member_variable1;
data_type member_variable2;
………………………………………..
data_type member_variableN;
} s1; //global structure variable declaration….
OR
struct structure_name structure variable; // local structure variable declaration
Create a Structure
struct abc
{
int roll_no;
float marks;
char name;
} s1; //global structure variable declaration
OR
struct abc s1; // local structure variable declaration
struct abc s1,s2,s3; //Multi Structure variable declaration
Initialization of a Structure
Syntax:
struct structure_name structure_variable={value1, value2,…., valueN};
There is a one to one correspondence between the members and their initializing values.
OR
struct structure_name structure_variable;
structure_variable.member_variable;
Example:
Initialization a Structure
//Structure is Created
struct abc
{
int roll_no;
float marks;
char name;
};
// Initialization a Structure
void main()
{
struct abc s1;
s1={1,80,’a’};
}
OR
// Initialization a Structure
void main()
{
struct abc s1;
printf(“Enter the Value of Structure members”);
scanf(%d%f%c”,&s1.roll_no,&s1.marks,s1.name);
}
Partial Initialization of Structure
We can initialize the first few members and leave the remaining blank.
However, the uninitialized members should be only at the end of the list.
The uninitialized members are assigned default values as follow:
-Zero for integer and floating point numbers.
-’\0’ for characters and strings.
Example:
Initialization a Structure
//Structure is Created
struct abc
{
int roll_no;
float marks;
char name;
};
// Initialization a Structure
void main()
{
struct abc s1;
s1={1,80};
}
OR
// Initialization a Structure
void main()
{
struct abc s1;
printf(“Enter the Value of Structure members”);
scanf(%d%f%c”,&s1.roll_no);
}
Accessing of Structure
Syntax :
structure_variable_name.structure_member_name
- By using dot (.) operator or period operator or member operator.
- Here, structure_variable_name refers to the name of a struct type variable and structure_member_name refers to the name of a member within the structure.
Example:
Accessing Structure Members
//Structure is Created
struct abc
{
int roll_no;
float marks;
char name;
};
// Initialization a Structure
void main()
{
struct abc s1;
s1={1,80,’a’};
// Accessing structure member
printf(“Enter the Value of Structure members are %d,%f and %c”, s1.roll_no,s1.marks,s1.name);
getch();
}
OR
// Initialization a Structure
void main()
{
struct abc s1;
s1={1,80,’a’};
// Accessing structure member
printf(“Enter the Value of Structure members are %d,%f and %c”, s1.roll_no,s1.marks,s1.name);
getch();
}
// Initialization a Structure
void main()
{
struct abc s1;
printf(“Enter the Value of Structure members”);
scanf(%d%f%c”,&s1.roll_no,&s1.marks,s1.name);
// Accessing structure member
printf(“Enter the Value of Structure members are %d,%f and %c”, s1.roll_no,s1.marks,s1.name);
getch();
}
Watch Video Here...