0% found this document useful (0 votes)
15 views

Lab 13 Structures

The document discusses structures in C programming. It defines a structure as a collection of elements of different data types. Structures allow grouping of related data and can contain nested structures. Structures are declared using the struct keyword and variables of a structure type can be initialized during declaration or later. Members of a structure are accessed using the dot operator. Arrays of structures can be used to store multiple records of the same type.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
15 views

Lab 13 Structures

The document discusses structures in C programming. It defines a structure as a collection of elements of different data types. Structures allow grouping of related data and can contain nested structures. Structures are declared using the struct keyword and variables of a structure type can be initialized during declaration or later. Members of a structure are accessed using the dot operator. Arrays of structures can be used to store multiple records of the same type.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 18

Structure

• Collection of elements with different datatype


• Define a structure
• Declare structure variables using that structure
• Initialization
• Access the members of structure
• Nested Structure
• Array of Structures
Define a structure
• Syntax:
struct tagname
{
Datatype variable name;
Datatype variable name;
..
..
};
Note-If we are not using tag name, then all the structure variables should mention
in the structure definition itself.
Example 1
struct student
{
int id;
char name[20];
float perc;
};
struct student s1; \\ declaration of structure variables
Example 2
struct
{
int id;
char name[20];
float perc;
} s1; \\ declaration of structure variables
Initialization
• Compile time initialization
• Eg- struct student s1={20,”Anu”, 75.5};
• struct student s2={24,”Abc”, 70.7};

OR
• Eg- s1.id=20;
• s1.name=“Anu”;
Access the members of the structures
• Access the members of the structures using dot operator
• Eg- s1.id
• s1.name
Run time initialization

• Example
• scanf(“%d %s%f”,&s1.id,s1.name,&s1.perc);
Example
#include<stdio.h>
void main()
{
struct student
{
int id;
char name[20];
float perc;
};
struct student s1={20,"Anu",75.5};
struct student s2;
printf("enter details of s2");
scanf("%d %s %f",&s2.id,s2.name,&s2.perc);
printf("Details of student1");
printf("student id=%d\n student name=%s\n student percentage=%f\n",s1.id,s1.name,s1.perc);
printf("Details of student2");
printf("student id=%d\n student name=%s\n student percentage=%f\n",s2.id,s2.name,s2.perc);
}
Nested Structure
• A structure variable as a member of another structure
Example
#include<stdio.h>
struct dob
{
int day;
int month;
int year;
};
struct student
{
int id;
char name[20];
struct dob d1;
};
struct student s1;
void main()
{
printf("enter id of s1");
scanf("%d",&s1.id);
printf("enter name of s1");
scanf("%s",s1.name);
printf("enter dob of s1");
scanf("%d %d %d",&s1.d1.day,&s1.d1.month,&s1.d1.year);
printf("Details of student1\n");
printf("student id=%d\n student name=%s\n student dob=%d-%d-%d\
n",s1.id,s1.name,s1.d1.day,s1.d1.month,s1.d1.year);
}
Output
Or write the nested structure example like
this..
struct student
{
int id;
char name[20];
struct dob
{
int day;
int month;
int year;
}d1;
};
Array of Structures
• To declare an array of structure, first the structure must be defined and then an array variable of
that type should be defined.
• For Example − struct book b[10]; //10 elements in an array of structures of type ‘book’
Example
struct employee{
char name[20];
int eid;
int salary;
};
void main()
{
struct employee emp[10];
Example
#include <stdio.h>
#include <string.h>
struct student{
int id;
char name[30];
float percentage;
};
int main(){
int i;
struct student record[2];
// 1st student's record
record[0].id=1;
strcpy(record[0].name, "Bhanu");
record[0].percentage = 86.5;
// 2nd student's record
record[1].id=2;
strcpy(record[1].name, "Priya");
record[1].percentage = 90.5;
// 3rd student's record
record[2].id=3;
strcpy(record[2].name, "Hari");
record[2].percentage = 81.5;
for(i=0; i<3; i++){
printf(" Records of STUDENT : %d \n", i+1);
printf(" Id is: %d \n", record[i].id);
printf(" Name is: %s \n", record[i].name);
printf(" Percentage is: %f\n\n",record[i].percentage);
}
return 0;
}
Output
Records of STUDENT : 1
Id is: 1
Name is: Bhanu
Percentage is: 86.500000
Records of STUDENT : 2
Id is: 2
Name is: Priya
Percentage is: 90.500000
Records of STUDENT : 3
Id is: 3
Name is: Hari
Percentage is: 81.500000
Exercises
• WAP in C to store information of a student(id, age, name) using
structure
• WAP in C to store information of 10 employees(eid, name, salary)
using structure
• WAP in C to store information of a student(id, name, age and
address(house _no, city name)) using structure

You might also like