# Structure-in-c .

In this session i have discussed structure in C .

Structure is a user defined data type in c . It is used to stored the various items with different data type .

i.e A person has name , address , salary , age etc . We can stored all in a structure .

Structure syntax

``` struct struct_name { int item1; char item2; float item3; }; ```

In above code use the struct keyword for declare the structure . struct_name is the name of structure . In body there are various items with different data types .

Declare the structure

``` struct Person { char name[20]; int age; float salary; }; ```

Create the struct variable

When a structure is defined, it creates a user-defined type. However, no storage or memory is allocated. To allocate memory of a given structure type and work with it, we need to create variables.

``` struct Person { char name[20]; int age; float salary; }; int main() { struct Person p1, p2, p3[20]; return 0; } ```

We have declared the p1 , p2 , p3[20] as struct variable .

How to access struct members .

There are two ways by which we can access the struct members .

1-Member operator .

2-Structure pointer operator ->

Suppose if you want to access the age of struct the write below code

p1.age

Example : Write a program demonstrate the structure in c .

``` #includ<stdio.h> #include<string.h> struct person { char name[20]; int age; int salary; }; int main() { struct person p; printf("Enter the name :"); scanf("%s",p.name); printf("Enter the age"); scanf("%d",&p.age); printf("Enter the salary"); scanf("%d",&p.salary); printf("-------------------------------\n"); printf("Name : %s\n",p.name); printf("Age : %d\n",p.age); printf("Salary : %d\n",p.salary); return (0); } ```
```    OUTPUT : Enter the Name : Rakesh
Enter the Age  : 20
Enter the Salary : 20000
-------------------------------------
Name : Rakesh
Age : 20
Salary : 20000
```

Example : Write a program that stored the two records in struct .

``` #includ<stdio.h> #include struct person { char name[20]; int age; int salary; }; int main() { int i; struct person p[2]; for(i=0;i<2;i++) { printf("Enter the name :"); scanf("%s",p[i].name); printf("Enter the age"); scanf("%d",&p[i].age); printf("Enter the salary"); scanf("%d",&p[i].salary); } printf("-------------------------------\n"); for(i=0;i<2;i++) { printf("Name : %s\n",p[i].name); printf("Age : %d\n",p[i].age); printf("Salary : %d\n",p[i].salary); } return (0); } ```
```    OUTPUT : Enter the Name : Rakesh
Enter the Age  : 20
Enter the Salary : 20000
Enter the Name : Shashank
Enter the Age  : 23
Enter the Salary : 30000

-------------------------------------
Name : Rakesh
Age : 20
Salary : 20000
Name : Shashank
Age : 21
Salary : 30000
```

# Welcome to AndroidATPoint.

`                     `