Ues103 L5
Ues103 L5
2
What is a Structure?
It is a convenient construct for representing a group of logically related data
items. Particularly relevant when the constituent data items are of different
types.
Compare with arrays that are collections of items of the same type.
Better readability even when the constituent data items are of the same type.
• Examples:
• Student name (string), roll number (string), height (float), and marks
(int).
• Real part and imaginary part of a complex number (pair of double).
Structures help in organizing composite data in a programmer-friendly
way. The individual structure elements are called members or fields.
This is our first look at a user-defined data type.
Defining a Structure
The composition of a structure may be defined as:
struct 〈 name of structure〉 {
For example:
struct point {
float xcoord;
float ycoord;
};
Example
A structure definition: • struct is the required C keyword
struct student { • Do not forget the ; at the end of the
char name[30]; structure defn
char roll_number[12];
• The individual members can be
float height;
int total_marks; ordinary variables, pointers, arrays,
}; or other structures (any data type)
• The member names within a particular
Defining structure structure must be distinct from one
variables: another
struct student a1, a2, a3;
• A member name can be the same as the
name of a variable defined outside the
A new data type structure
Structure Definition versus Structure Variable Declaration
Each structure type variable (like a, b, c) has its own copy of each member of that structure type.
TypeDefinitions
The typedef construct
The typedef construct can be used to give new names to (existing) data types in C.
struct point {
float xcoord;
float ycoord;
} a, b;
a.xcoord = 2.5; a.ycoord = 3.2;
b.xcoord = b.ycoord = 0;
Structure initialization
Structure variables may be initialized following similar
rules of an array. The values are provided within 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;
Example: Addition of two complex numbers
#include<stdio.h> int
main( )
{
struct complex {
float real; Structure ation can be outside
float imag; declar as well. main() necessary if a
} a, b, c; This is program has ns using a
multiple structure type.
functio
scanf ("%f %f", &a.real, &a.imag);
scanf ("%f %f", &b.real, &b.imag);
This is allowed !!
struct student {
char name[50];
float CGPA;
int height;
} s;
#include <stdio.h>
// compute average cgpa
struct student{ avg = 0.0;
float cgpa;
for (i=0;i<5;i++)
char name[10];
avg += st[i].cgpa;
};
int main(
avg = avg / 5.0;
{
printf ("Avg cgpa:%f",
int i; float avg;
avg);
struct student st[5];
printf("Enter records of 5 students\n");
for (i=0; i<5; i++) { return 0;
}
printf ("Enter Cgpa:"); Note: &st[i].cgpa is to be
scanf ("%f",&st[i].cgpa); interpreted as &(st[i].cgpa),
printf ("Enter Name:");
not as (&st[i]).cgpa (an
scanf ("%s”,st[i].name);
address cannot have a member).
}
A structure may contain other structures
typedef struct {
double x, y;
} point;
typedef struct {
point A, B, C;
double area;
} triangle;
It is not allowed that struct a contains struct b, and struct b contains struct a.
Such pointers are used extensively to create chains and other types of linked data
structures.
typedef struct _student { // A name after struct is
mandatory
char name[50];
float CGPA;
struct _student *next; // A self-referencing pointer
} student;
Structures and functions
Structures are passed by value to functions
void print ( COMPLEX a )
#include <stdio.h> {
printf ("(%f, %f) ",
typedef struct { a.real, a.imag);
float real; }
float imag;
} COMPLEX; main( )
{
COMPLEX x = {4.0, 5.0},
void swap ( COMPLEX a, COMPLEX b ) y = {10.0, 15.0};
{
COMPLEX tmp; print(x); print(y); printf("\n");
swap(x, y);
tmp = a; a = b; b = tmp; print(x); print(y); printf("\n");
} }
Program output
No swapping takes place actually,
(4.000000, 5.000000) (10.000000, 15.000000)
(4.000000, 5.000000) (10.000000, 15.000000) similar to what we saw for integers,
floats, and so on.
Structures can be returned from functions
#include <stdio.h> main( )
{
COMPLEX x = {4.0, 5.0},
typedef struct { y = {10.0, 15.0};
float real; COMPLEX z;
float imag;
z = add(x, y);
} COMPLEX; printf (" %f, %f \n",
z.real, z.imag);
}
COMPLEX add ( COMPLEX a, COMPLEX b )
{
COMPLEX tmp;
tmp.real = a.real + b.real;
tmp.imag = a.imag + b.imag; Program output
return tmp;
}
14.000000, 20.000000
Pointers to Structures
Pointers and Structures
struct class {
int roll;
char name[20];
float marks;
};
Once ptr points to a structure variable, the members can be accessed as: