Structure is a group of data items which are of different datatype which were held together in a single unit. We can make a structure datatype by using the keyword "struct".
We can use either Structure or Unions as per our requirement, it depends on the work we want to make our program do.
Syntax of Structure: struct struct_name
{
datatype var 1;
datatype var 2;
datatype var 3;
:
:
datatype var n;
};
How to declare the variable of the structure ?
There are two ways we can declare an structure:-
We can use either Structure or Unions as per our requirement, it depends on the work we want to make our program do.
Why Structures ?As we are using array to store data items in C, but we know that an array can store only similar type of datatypes in one unit i.e, we cannot store the data item of different datatype in one unit. So to overcome these type of problems the idea of Structures were introduced.
Syntax of Structure: struct struct_name
{
datatype var 1;
datatype var 2;
datatype var 3;
:
:
datatype var n;
};
How to declare the variable of the structure ?
There are two ways we can declare an structure:-
- Either we declare the variable on the time we have declared the structure datatype on the very end of it as shown below-
struct Student
{
int roll _no;
char name[20];
int age;
}s1,s2,s3.....sn; - Or we can declare the variable after we have declared the structure datatype as below-
struct Student
{
int roll _no;
char name[20];
int age;
};
int main( )
{
struct Student s1,s2,s3........sn;
----------------------------------
----------------------------------
----------------------------------
return 0;
}
Accessing the Structure
So after declaration of the struct datatype, now we have to enter the values on their respective places. And to enter the value(by user) or to display the values of the variables of structure datatype we use " • "(dot) operator.
i.e, structure_name.variable_name
EXAMPLE:-
OUTPUT:
Even without " • " operator we can also enter the values in a structure but that value is not entered by the user, instead the values were pre-declared in the program.
i.e, struct struct_name variable_name={"value 1","value 2","value 3"};
EXAMPLE:-
OUTPUT:
For giving some suggestion please comment us or check our "Contact us" page. Also more update related to coding will be added in this Blog later on, so please get attached with this Blog by clicking the follow button. Thank you for your support and time...i.e, structure_name.variable_name
EXAMPLE:-
OUTPUT:
Even without " • " operator we can also enter the values in a structure but that value is not entered by the user, instead the values were pre-declared in the program.
i.e, struct struct_name variable_name={"value 1","value 2","value 3"};
EXAMPLE:-
OUTPUT:
Nested Structure
There are two ways to declare a structure within structure:-
- Declare two separate structure:
In this we declare two different structure individually and then use one structure as a variable inside another structure-
struct S1
{
int a;
float b;
char c;
};
struct S2
{
char aa[10];
int ab;
int ac;
struct S1 ad;
};
So here we are using variable "ad" of struct S1 datatype, but both the structures are defined separately but are Nested together. - Embedded Structure:
In this case a structure is defined inside a different structure directly.
struct S2
{
char aa[10];
int ab;
int ac;
struct S1
{
int a;
float b;
char c;
}ad;
};
Here we can see that structure S1 is defined inside the structure S2, so this is embedded way to make a Nested Structure.
Things To Remember
- After the declaration of any Structure semicolon( ; ) is must.
- It will not return NULL at the when their is lack of memory.
- If memory is not available then it simply doesn't execute.
- Entering of the value should be in same sequence/order as written in the program.
0 Comments