1 Structure
1 Structure
Structure
Array is used to represent a group of data items (elements)
having same data type such as int, float etc. If we want to represent a
collection of data items (elements) of different data types within a
single name then we cannot use an array for that ‘C’ supports a data
type known as “structure”.
** definition of structure
“A structure is a user defined data type which contains
similar or different data types.
2) inside main()
Syntax
struct structure_variable_name1… structure_variable_namen;
e.g struct student p;
**Example
#include<stdio.h>
struct student
{
int rno;
char name[20];
};
int main()
{
struct student p; // structure_variable_name
printf(“enter rno”);
scanf(“%d”,&p.rno);
printf(“enter name”);
scanf(“%s”,p.name);
printf(“\n rno is %d”,p.rno);
printf(“\n name is %d”,p.name)
return 0;
}
**Accessing structure members/elements
We can access structure elements by using member operator
‘.’, which is also called as “dot operator or period operator”.
In the above example we have read rno and name by using
scanf () and write (display) rno and name by using printf() with
dot(.) operator.