What is structure
Structure is a group of
similar and dissimilar data types. Structure is user defined data type. To
create a structure, we use struct keyword. Structure starts with a
delimiters({) and close with a delimiter (}). After closing the delimiters, we
place a (;) semicolon. To access the elements of the structures, we create a
structure variable/ structure field, with the help of structure variable we use
the dot (.) operator and a specific element name, which we want to access. As
shown in the syntax below:
‘
struct<structure
name>
{
<data
type><variable>;
<data
type><variable>;
};
Write a program using with
structure.
#include<stdio.h>
#include<conio.h>
struct student
{
char name[20];
int roll, marks;
};
void main()
{
struct student s;
clrscr();
printf(“Enter student name”) ;
scanf(“%s”,&s.name);
printf(“Enter student roll
no”);
scanf(“%d”,&s.roll);
printf(“Enter student
marks”);
scanf(“%d”,&s.marks);
print(“\n your name=%s”,s.name);
printf(“\n your roll=%d”,s.roll);
printf(“\n your marks=%d”,s.marks);
getch();
}
Copy Structure
We can copy the value of one structure variable into another
structure variable of same structure it means we can create the two variable of
a single structure and copy the values of first variable to second variable
using the assignment operator.
Nested Structure
Nested structure means a structure inside a structure. It is
possible to have one structure a as member of another structure .In nested
structure the first structure is called the outer structure and second
structure which is inside the structure is known
as inner structure.
Nested means something inside another thing.
Advantages of Structure
- If we want to store a record of person we can use structure for example:-
If we want to store the details of
a student like his name, roll, marks etc.
- If we want to store different type of element into a single unit.
- We can pass structure as argument into the function in place of passing more then one arguments. They can pass a single unit.
- It’s a collection of heterogeneous but related elements, which is called record. .








0 comments:
Post a Comment