POP Module 5
POP Module 5
• The variables that are used to store the data are called members of the
structure.
• Structure definition is terminated with a semicolon.
• We can create the structure for a person as shown below:
DECLARATION OF STRUCTURE VARIABLES
• Here the above statement declares that the variables s1 and s2 are variables
of type STUDENT(which is of type as struct student).
• Typedef existing datatype new_data_type
• Typedef int INTERGER;
Accessing Members of a Structure
Example:
struct student s1={“CANARA”,”4CB”,18,25.0};
• Reading and writing structure variable:
Based on the member type structure members can use input and output
statements for reading writing.
printf(“\n the student details are”);
printf(“\n Name: %s”,s.name);
printf(“\nUSN: %s”,s.usn);
printf(“\n age:%d”,s.age);
printf(“\n marks: %f”, s.marks);
}
STRUCTURES WITHIN STRUCTURES (NESTED STRUCTURES):
A structure within a structure is called nested structure i.e. a structure is a
member of another structure.
We can create nested structures in two ways
1. Placing complete definition of a structure inside the definition of another
structure.
2. Structure is defined separately and a variable of structure can be placed
inside the definition of another structure definition.
• ACCESSING NESTED STRUCTURE MEMBERS:
– SYNTAX:
– EXAMPLE:
S.dob.day=20;
S.dob.month=3;
S.dob.year=2018;
• Array of structures:
An array of structure is declared in the same way as we had declared
array of built in data type.
Syntax:
Passing individual member
#include<stdio.h>
typedef struct
{
int x;
int y;
}POINT;
Void display(int a,int b);
Main()
{
POINT p1={2,3};
display(p1.x,p1.y);
}
Void display(int a,int b)
{
printf(“the coordinates of the pointare:%d %d”,a,b);
Passing the entire structure
#include<stdio.h>
Typedef struct
{
int x;
int y;
}POINT;
Void display(POINT);
Main()
{
POINT p1={2,3};
display(p1);
}
Void display(POINT p)
{
Printf(“%d%d”,p.x,p.y);
}
union
Union is collection of variables of different data type, you can
store information in one field at any one time.
Declaring a union
union union_name
{
data_type var_name;
data_type var_name;
};
#include<stdio.h>
typedef Union point1
{
Int x;
Int y;
};
Mian()
{
point p1;
P1.x=4;
P1.y=5;
}
Struct student
{
union
{
Char name[20];
Int roll;
};
Int marks;
};
Main()
{
Struct student stud;
Enum enumaration_name{identifier1, identifier2….};
Enum colour{red=2,blue,black=7};
Stdin- resd operation
Stdout- write operation
Stderr- write operation
Types of file
1. Text
2. Binar
Uses of files
steps
3. Declare a file pointer variable FILE *file_pointer_name; FILE *fp;
4. Open the file FILE *fopen(const char *filename, const chat *mode)
5. Fp=fopen(“student.DAT”,”r+”);
6. Process the file
7. Close the file fclose()
8. C==‘\0’
9. C==EOF