Module5-chapter1
Module5-chapter1
struct struct–name
{
data_type var–name;
data_type var–name;
...............
};
For example:
struct student
{
int r_no;
char name[20];
char course[20];
float fees;
};
1
5.1.2 Typedef Declarations
The typedef (derived from type definition) keyword enables the programmer to create a new data
type name by using an existing data type alternate name is given to a known data type.
The general syntax of using the typedef keyword is given as:
typedef existing_data_type new_data_type;
For example: typedef int INTEGER;
then INTEGER is the new name of data type int.
To declare variables using the new data type name, precede the variable name with the data type
name (new).
Therefore, to define an integer variable, INTEGER
num=5; For example, consider the following declaration:
typedef struct student
{
int r_no;
char name[20];
char
course[20];
float fees;
};
2
char name[20];
char
course[20];
float fees;
}stud1 = {01, "Rahul", "BCA", 45000};
Or,
3
by writing,
struct student stud1 = {01, "Rahul", "BCA", 45000};
4
1. Simple Addition
In this techniques, make a list of all data types and add the memory required by each.
Consider a simple structure of an employee
5
struct Employee
{
int emp_id; char
name[20];
double salary;
char designation[20];
float experience;
};
Size = size of emp_id+ size of name + size of salary + size of designation + size of experience Size
of emp_id = 2
Size of name =20* size of character Size
of salary = 8
Size of designation = 20 * Size of character Size
of experience = 4
Therefore, Size = 2 + 20*1 +8 +20 *1+4
= 2 +20+8+20+4
= 54bytes
2. Using sizeof operator
The sizeof operator is used to calculate the size of a data type, variable, or an expression.
This operator can be used as follows:
sizeof(struct_name);
Ex:
#include<stdio.h>
struct employee
{
int emp_id; char
name[10];
double salary;
char designation [20];
float experience;
};
void main()
{
struct employee e; printf(“%d”,
sizeof(e));
}
Example: C program to read and display the student details using structures. #include<stdio.h>
struct student
{
int rnum;
char name[20]; int
marks;
}s[60];
void main()
{
int i,n;
printf("Enter the number of students");
6
scanf("%d",&n);
}
Output:
Enter the number of students: 5
Enter the roll number,
Name, Marks and Grade of
Student 1 details
14
Dhavan
89
Student 2 details
15
Karan
55
Student 3 details
11
Deepa
45
Student 4 details
12
Lakshmi
35
Student 5 details
10
Soma
68
Student Details are:
Roll_number Name Marks
14 Dhavan 89
15 Karan 55
11 Deepa 45
12 Lakshmi 35
10 Soma 68
7
5.2 Structures and Functions
A function may access the members of a structure in three ways.
void main()
{
POINT p1 = {2, 3};
display(p1.x, p1.y);
}
#include <stdio.h>
typedef struct
{
int
x;
int
8
y;
}POINT;
void display(POINT);
9
void main()
{
POINT p1 = {2, 3};
display(p1);
}
void display(POINT p)
{
printf("The coordinates of the point are: %d %d", p.x, p.y);
}
Example: Write a program using pointer to structure to initialize the members in the
structure.
#include<stdio.h>
struct student
{
int r_no;
char name[20];
char course[20];
float fees;
};
void main()
{
struct student stud1, *ptr_stud1;
ptr_stud1 = &stud1;
ptr_stud1->r_no = 01;
strcpy(ptr_stud1->name, "Rahul");
strcpy(ptr_stud1->course, "BCA");
10
ptr_stud1->fees = 45000;
printf("\n DETAILS OF STUDENT");
printf("\n ");
printf("\n ROLL NUMBER = %d", ptr_stud1-
>r_no); printf("\n NAME = %s", ptr_stud1->name);
printf("\n COURSE = %s", ptr_stud1->course);
printf("\n FEES = %f", ptr_stud1->fees);
}
5.3 Union
Like structure, a union is a collection of variables of different data types. The only difference
between a structure and a union is that in case of unions, you can only store information in one field
at any one time.
To better understand union, think of it as a chunk of memory that is used to store variables of
different types. When a new value is assigned to a field, the existing data is replaced with the new
data.
12
void main()
{
POINT1 P1 = {2,3};
// POINT2 P2 ={4,5}; Illegal with union
POINT2 P2;
P2. x = 4;
P2.y = 5;
printf("\n The co-ordinates of P1 are %d and %d", P1.x, P1.y);
printf("\n The co-ordinates of P2 are %d and %d", P2.x, P2.y);
}
Output:
The co-ordinates of P1 are 2 and 3
The co-ordinates of P2 are 5 and 5
14
}
16
5.5.4 Enumeration Type Conversion
Enumerated types can be implicitly or explicitly cast.
For ex, the compiler can implicitly cast an enumerated type to an integer when required.
However, when we implicitly cast an integer to an enumerated type, the compiler will either generate
an error or warning message.
To understand this, answer one question. If we write:
enum COLORS{RED, BLUE, BLACK, GREEN, YELLOW, PURPLE, WHITE};
enum COLORS c;
c = BLACK + WHITE;
Here, c is an enumerate data type variable. If we write, c = BLACK + WHITE, then logically, it
should be 2 + 6 = 8; which is basically a value of type int. However, the left hand side of the
assignment operator is of the type enumCOLORS. SO the statement would complain an error.
To remove the error, you can do either of two things. First, declare c to be an int.
Second, cast the right hand side in the following manner. :
c = enum COLORS(BLACK + WHITE);
17
5.5.7 Differences between structure and union
Structure Union
The struct keyword is used to define it. The union keyword is used to define it.
Syntax: Syntax:
struct struct–name union union-name
{ {
data_type var–name; data_type var-name;
data_type var–name; data_type var-name;
............... ……….
}; };
Example: Example:
struct student union student
{ {
int r_no; int r_no;
char name[20]; char name[20];
char course[20]; char course[20];
float fees; float fees;
}; };
A variable of structure student can be defined by A variable of union student can be defined by
writing: writing:
struct student stud1; union student stud1;
Several members of a structure can be initialized at Individual members of a structure can be initialized
once. one by one.
Ex: Ex:
struct student stud1 = {01, "Rahul", "BCA", union student stud1;
45000}; stud1. r_no=01;
stud1. name=
"Rahul"; stud1.
course="BCA"; stud1.
fees =45000;
Each member within structure is assigned unique The members or fields share the same memory
storage area of location. space, so fresh data replaces any existing data.
The size of the structure is the sum of all members The size of the union is the size of the largest
or fields. member or field.
Altering the value of a member will not affect other Altering any value of a member will alter other
members of the structure. member values.
18
inprotected.com