Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
2 views

Module 4 Structures

Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views

Module 4 Structures

Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, 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 structure is a collection of variables of
• different data types under a single name.
• The variables 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 not occupy
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:
5
struct student s1, s2, s3;
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 definition and The use of structure_name is
variable declaration be
can
combined as: optional. 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
long int phone_no;
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 same / Collection of variables of


similar data type different / dissimilar data types
Ex: Ex:
int a[10]; 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;
printf("\n\nEnter Student Roll : ");
char Name[25]; scanf("%d",&S.Roll);
int Marks[3]; //Statement 1 : array of marks
printf("\n\nEnter Student Name : ");
int Total; 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 \
int n,i;
n”,e[i].eid,e[i].name,e[i].salary);
int eid;
printf(“Enter the no.of employees\n”);
char name[20]; scanf(“%d”,&n);
printf(“Employee salary above 10000\n”);
int salary; printf(“Enter the Employee details:\n”);
for(i=0;i<n;i++)
{
}; for(i=0;i<n;i++)
if(e[i].salary>10000)
{
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