2. 2
What is a Structure?
Used for handling a group of logically
related data items
Examples:
Student name, roll number, and marks
Real part and complex part of a complex number
Helps in organizing complex data in a more
meaningful way
The individual structure elements are called
members
3. 3
Defining a Structure
struct tag {
member 1;
member 2;
:
member m;
};
struct is the required C keyword
tag is the name of the structure
member 1, member 2, … are individual member
declarations
4. 4
Contd.
The individual members can be ordinary variables,
pointers, arrays, or other structures (any data type)
The member names within a particular structure
must be distinct from one another
A member name can be the same as the name of
a variable defined outside of the structure
Once a structure has been defined, the individual
structure-type variables can be declared as:
struct tag var_1, var_2, …, var_n;
5. 5
Example
A structure definition
struct student {
char name[30];
int roll_number;
int total_marks;
char dob[10];
};
Defining structure variables:
struct student a1, a2, a3;
A new data-type
6. 6
A Compact Form
It is possible to combine the declaration of the structure
with that of the structure variables:
struct tag {
member 1;
member 2;
:
member m;
} var_1, var_2,…, var_n;
Declares three variables of type struct tag
In this form, tag is optional
8. 8
Structure Initialization
Structure variables may be initialized following
similar rules of an array. The values are provided
within the second braces separated by commas
An example:
struct complex a={1.0,2.0}, b={-3.0,4.0};
a.real=1.0; a.imag=2.0;
b.real=-3.0; b.imag=4.0;
9. 9
Accessing a Structure
The members of a structure are processed
individually, as separate entities
Each member is a separate variable
A structure member can be accessed by writing
variable.member
where variable refers to the name of a structure-type
variable, and member refers to the name of a
member within the structure
Examples:
a1.name, a2.name, a1.roll_number, a3.dob
11. 11
Operations on Structure
Variables
Unlike arrays, a structure variable can be directly
assigned to another structure variable of the same type
a1 = a2;
All the individual members get assigned
Two structure variables can not be compared for
equality or inequality
if (a1 == a2)…… this cannot be done
13. 13
: 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");
scanf("%d",&n); printf("nEnter the roll number, Name , Marksn");
for(i=1;i<=n;i++)
{
printf("nStudent %d detailsn",i);
scanf("%d",&s[i].rnum);
scanf("%s",s[i].name);
scanf("%d",&s[i].marks);
}
printf("nStudent Details are:");
printf("nRoll_numbertNametMarks");
for(i=1;i<=n;i++)
printf("n%dtt%st%dn",s[i].rnum,s[i].name,s[i].marks);
}
15. 15
Parameter Passing in a
Function
Structure variables can be passed as parameters like
any other variables. Only the values will be copied
during function invocation
20. 20
Defining data type: using typedef
One may define a structure data-type with a single
name
typedef struct newtype {
member-variable1;
member-variable2;
.
member-variableN;
} mytype;
mytype is the name of the new data-type
Also called an alias for struct newtype
Writing the tag name newtype is optional, can be
skipped
Naming follows rules of variable naming
21. 21
typedef : An example
typedef struct {
float real;
float imag;
} _COMPLEX;
Defined a new data type named _COMPLEX. Now can
declare and use variables of this type
_COMPLEX a, b, c;
22. 22
Note: typedef is not restricted to just structures,
can define new types from any existing type
Example:
typedef int INTEGER
Defines a new type named INTEGER from the
known type int
Can now define variables of type INTEGER which
will have all properties of the int type
INTEGER a, b, c;
23. 23
The earlier program using typedef
typedef struct{
float real;
float imag;
} _COMPLEX;
void swap (_COMPLEX a, _COMPLEX b)
{
_COMPLEX tmp;
tmp = a;
a = b;
b = tmp;
}
25. 25
Output:
(4.000000, 5.000000)
(10.000000, 15.000000)
(4.000000, 5.000000)
(10.000000, 15.000000)
x and y are not swapped! But that has got
nothing to do with structures specially. We
will see its reason shortly
26. 26
Exercise Problems
1. Extend the complex number program to include functions
for addition, subtraction, multiplication, and division
2. Define a structure for representing a point in two-
dimensional Cartesian co-ordinate system
• Write a function to compute the distance between two
given points
• Write a function to compute the middle point of the line
segment joining two given points
• Write a function to compute the area of a triangle, given
the co-ordinates of its three vertices