0% found this document useful (0 votes)
2 views17 pages

Structure 4

This document provides an overview of structures in programming, detailing their definition, syntax, and usage. It explains how to define a structure, access its members, and create arrays within structures, along with examples of employee records and student details. The document emphasizes the importance of structures as user-defined data types that group related data of different types.

Uploaded by

sinchanam7006
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views17 pages

Structure 4

This document provides an overview of structures in programming, detailing their definition, syntax, and usage. It explains how to define a structure, access its members, and create arrays within structures, along with examples of employee records and student details. The document emphasizes the importance of structures as user-defined data types that group related data of different types.

Uploaded by

sinchanam7006
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 17

MODULE-IV

Structures: Basics of Structures, Nested Structures, Arrays of Structures, Arrays within structures,
Structures and functions, pointers and structures, Self-referential structures, Unions.
Introduction to Structure
• Problem:
• – How to group together a collection of data items of different
types that are logically related to a particular entity???
Structure
•A is a collection of variables of
structure
• different data types under a single name.
• Thevariables are called members of the structure.
• The structure is also called a user-defined data type.

3
Defining a Structure
• Syntax:
struct structure_name
{
data_type member_variable1;
data_type member_variable2;
………………………………;
data_type member_variableN;
};
Once structure_name is declared as new data type, then
variables of that type can be declared as:
struct structure_name structure_variable;
Note: The members of a structure do notoccupy
memory until they are associated with a structure_variable.
4
• Example
struct student
{
char name[20];
int roll_no;
float marks;
char gender;
long int phone_no;
};

struct student st;


• Multiple variables of struct student type can be declared
as:
struct student s1, s2, s3;
5
Defining a
structure…
• Each variable of structure has its own copy of
member variables.
• The member variables are accessed using the
dot (.) operator or member operator.
• For example: s1.name is member variable
name of s1 structure variable while s3.gender
is member variable gender of s3 structure
variable.

6
Defining a
structure…
• The structure and The use of structure_name is
variable declaration can
definition be optional.
combined as: struct
struct student {
{ char name[20];
char name[20]; int roll_no;
int roll_no; float marks;
float marks; char gender;
char gender; long int phone_no;
long int phone_no; }st1, st2, st3;
}st1, st2, st3;

7
ACCESSING MEMBERS OF STRUCTURE :
To access the members of the structure, a variable for the structure
has to be created.

Syntax :
Struct tag variablename;

Ex: struct tree t;


Output:
Enter details ABC 18 M
The details are:
Name = ABC Age = 18 Gender = M.

ARRAYS STRUCTURES

Collection of variables of Collection of variables of


same / similar data type different / dissimilar data
Ex: types
int a[10]; Ex:
struct student
float p[40]; {
char name[20];
int age;
float marks;
};
Array within Structure
As we know, structure is collection of different data type. Like normal
data type, It can also store an array as well.
Syntax for array within structure

struct struct-name
{
datatype var1; // normal variable
datatype array [size]; // array variable
----------
----------
datatype varN;
};

struct struct-name obj;


Example for array within structure
void main()
struct Student {
int i;
{
struct Student S;
int Roll;
char Name[25]; printf("\n\nEnter Student Roll : ");
scanf("%d",&S.Roll);
int Marks[3]; //Statement 1 : array of marks
int Total; printf("\n\nEnter Student Name : ");
scanf("%s",&S.Name);
float Avg;
}; S.Total = 0;

for(i=0;i<3;i++)
{
printf("\n\nEnter Marks %d : ",i+1);
scanf("%d",&S.Marks[i]);
In the above example, we have created an
array Marks[ ] inside structure representing S.Total = S.Total + S.Marks[i];
}
3 marks of a single student. Marks[ ]
is now a member of structure student and to S.Avg = S.Total / 3;
access Marks[ ] we have used dot operator(.)
printf("\nRoll : %d",S.Roll);
along with object S. printf("\nName : %s",S.Name);
printf("\nTotal : %d",S.Total);
printf("\nAverage : %f",S.Avg);
}
Output :

Enter Student Roll : 10


Enter Student Name : Kumar
Enter Marks 1 : 78
Enter Marks 2 : 89
Enter Marks 3 : 56

Roll : 10
Name : Kumar
Total : 223
Average : 74.00000
Write a C program to maintain record of N Employee detail using array of structure with 3
fields: ID, Name and Salary And print details of employees whose salary is above 10000.=

int main()
#include<stdio.h> {
printf(“The Employee Details:\n”);
printf(“Eid \t Name \t Salary\n”);
struct employee for (i=0;i<n;i++)
struct employee e[10];
{ printf(“%d \t %s \t %d \n”,e[i].eid,e[i].name,e[i].salary);
int n,i;
int eid;
printf(“Enter the no.of employees\n”);
printf(“Employee salary above 10000\n”);
char name[20]; scanf(“%d”,&n);
for(i=0;i<n;i++)
int salary; {
printf(“Enter the Employee details:\n”);
if(e[i].salary>10000)
}; for(i=0;i<n;i++)
printf(“%s\n”,e[i].name);
{
}
printf(“Enter %d employee detail:\n”);
}
printf(“\nEnter the EID: ”);
scanf(“%d”,&e[i].eid);
printf(“\nEnter the Name: ”);
scanf(“%s”,e[i].name);
printf(“\nEnter the salary: ”);
scanf(“%d”,&e[i].salary);
}
Enter the no.of employees Enter the salary: 2000 The Employee Details:
3 Enter 3 employee detail: Eid Name Salary
11 arun 12000
Enter the Employee Enter the EID: 33 22 rao 2000
details:
33 bharath 25000
Enter 1 employee detail: Enter the Name: bharath Employee salary above 10000
arun
Enter the salary: 25000 bharath
Enter the EID: 11

Enter the Name: arun

Enter the salary: 12000


Enter 2 employee detail:

Enter the EID: 22

Enter the Name: rao

You might also like