Structures Session 4
Structures Session 4
STRUCTURES
Definition, Declaration, accessing structures, initialization, operations on structures, structures containing
arrays, structures containing pointers, nested structures, self-referential structures, arrays of structures,
structures and functions, structures and pointers.
Array of Structures
✓ In array of structures, the variable of structure is array .
✓ In our sample program, to store details of 100 students we would be required to use 100 different
structure variables from s1 to s100,which is definitely not very convenient. A better approach would
be to use an array of structures.
Syntax for declaring structure array
struct struct-name
{
datatype var1;
datatype var2;
datatype varN;
};
struct struct-name structure_variable [ size ];
Sample Program:
Define a structure for student which include roll number ,name,age and marks . Write a program to
read and display the information of ‘n’ number of students where n is value supplied by user.
#include<stdio.h>
#include<string.h>
struct student
{
int rno;
char name[10];
int marks,age;
};
void main()
{
struct student s[10];//Declares array of 10 student.
int i,n;
printf("\n enter number of students: ");
scanf("%d",&n);
for(i=0;i<n;i++)
{
//reading values for s3 using standard input function.
printf("\n enter rno,name ,marks,age of student %d: ",i+1);
scanf("%d%s%d%d",&s[i].rno,s[i].name,&s[i].marks,&s[i].age);
Details of student 1:
roll number: 2
name :Gandhi
marks :89
age: 18
Details of student 2:
roll number: 5
name :Raj
marks :76
age: 18
Details of student 3:
roll number: 6
name :Ram
marks :86
age: 18
s[0] s[1] s[2] s[3] s[4] s[5] s[6] s[7] s[8] s[9]
struct node
{
int data;
struct node *link;
};
int main()
{
struct node n1,n2;
n1.data=10;
n1.link=NULL;
n2.data=20;
n2.link=NULL;
n1.link=&n2;
return 0;
}