23
INTRODUCTION: A structure is a collection of elements of different types of data. Ex: consider the student information roll number, gender, age, height and weight of different types. main() { struct student { int rno; char gender; int age; rno gender age ht wt 2 1 2 4 4 13 bytes of memory is allocated in s1

Unit4 (2)

  • Upload
    mrecedu

  • View
    248

  • Download
    0

Embed Size (px)

DESCRIPTION

 

Citation preview

Page 1: Unit4 (2)

INTRODUCTION:

A structure is a collection of elements of different types of data.

Ex: consider the student information roll number, gender, age, height and weight of different types.

main()

{ struct student

{

int rno;

char gender;

int age;

rno gender

age ht wt

2 1

2 4 4

13 bytes of memory is allocated in s1

Page 2: Unit4 (2)

float ht;

float wt;

};

struct student s1,s2;

}

age gender

age ht wt

2 1

2 4 4

13 bytes of memory is allocated to s2

When all these data items are grouped under on single variable name, we can get some meaningful information.

struct is a keyword used to create a new user defined data type.

In some other high level programming languages the structures are named as records

Page 3: Unit4 (2)

DEFINING A STRUCTURE:

syntax:

struct user defined datatype

{

datatype member1;

datatype member2;

…………….

…………

datatype membern:

};

Example: struct student { int rno; int age; char gender; float ht; float wt;};

Page 4: Unit4 (2)

In the given syntax struct is keyword used to create new user defined data type.

An user defined data type name may be any valid ‘c’ identifier ( user defined name).

The data type may be a valid simple data type or compound data type.

The member names may be any valid ‘C’ identifiers .in the given example student is a new user defined data type name.

The variable rno, age, gender, ht, wt, are the structure members.

All these structure members must be enclosed within a pair of curly braces.

The structure definition should be terminated with semicolon.

Page 5: Unit4 (2)

DECLARING STRUCTURE VARIABLE:

Once a new user defined datatype is created by using the keyboard struct then we can create any no. of structure variables of that type.

syntax:

struct user defined_data type name

structure var1, structure var2,……….. Struct varn;

Above statement is called declarative statement of structure variables and it allocates memory space to these variables.

We can also declare structure variables immediately after structure definition.

Page 6: Unit4 (2)

ACCESSING STRUCTURE MEMBERS:

.

Structure member accessing operator

S1.rno=100Structure variable

Structure member

S1.gender=‘m’; s1.age=25;

S1.ht=5.5; s1.wt=60.2;

•Two operators are used to access member of structure. The structure member operator(.) also called dot operator and the structure pointer operator () also called the arrow operator.

•The structure member operator accesses a structure member via structure variable name.

Page 7: Unit4 (2)

syntax:

Ex: s1.rno, s1.age, s1.gender, s1.ht, s1.wt

rno gender

age ht wt wt

rno gender

age ht

100 M

25 60.25.5

s1 s2

S1.rno=100; s1.gender=M; s1.age=25; s1.ht=5.5; s1.wt=60.2

ASSIGNING VALUES TO STRUCTURE MEMBERS:

By using the assignment operator(=) we can assign values to all structure members of a structure variable.

Syntax: structure variable.member = value;

Ex: s1.rno= 100; s1.age=25

Page 8: Unit4 (2)

INITIALIZING A STRUCTURE VARIABLE:

Structures can be initialized using initialize lists as with arrays. To initialize a structure follow the variable name in the structure declaration with an equal sign and brace enclosed comma separated list of initializes.

Ex: struct student s1={100, 25,’M’, 5.5,65.2};

int x = 10;

int a[10]={ 10,20,30};

READING AND DISPLAYING THE STRUCTURE VARIABLES:

The ‘C’ will not read or write an entire structure as a single command. It will read or write the members of a structure as follows:

ex: scanf(“ %d, %d, %c, %f” ,&s1.rno,&s1.age,&s1.gender,&s1.ht);

printf(“ %d %d %c %f”, s1.rno, s1.age, s1.gender, s1.ht);

Page 9: Unit4 (2)

PROGRAM USING STRUCTURES:

Reads the details of 2 students and calculate total and average marks of 2 students.

#include<stdio.h>

#include<conio.h>

void main()

{ /* defining a structure */

struct student

{ int rno;

char name[30];

float m1,m2,m3;

float tot, avg;

};

Page 10: Unit4 (2)

/* 2. declaration of structure var*/

struct student s1,s2; (1st executed)

/* 3.reading 1st student details*/

printf(“ enter rno,name,m1,m2,m3 of first student \n”);

scanf(“ %d %s %f %f %f ”, &s1.rno,&s1.name,&s1.m1,&s1.m2,&s1.m3);

/* 4. reading 2nd student details*/

printf(“ enter rno, name, m1, m2, m3,of second student \n);

scanf(“ %d %s%f %f %f”, &s2.rno, &s2.name,&s2.m1,&s2.m2,&s2.m3);

/* calculate total and avg of first student marks*/

s1.tot= s1.m1+s1.m2+s1.m3;

s1.avg = s1.tot/3;

Page 11: Unit4 (2)

/* 6.calculate total and avg of first student marks*/ s2.tot= s2.m1+s2.m2+s2.m3; s2.avg = s2.tot/3;/* displaying first student details*/printf(“ first student details are \n”);printf( “roll no:%d\n, name:%s \n, m1: %f \n. m2:%f, m3:%f \n”, s1.rno, s1.name, s1.m1, s1.m2, s1.m3);printf(“ total :%f \n , average: %f \n”, s1.tot, s1.avg);

/* 8. displaying second student details*/

printf(“ second student details are:\n);printf( “roll no:%d\n, name:%s \n, m1: %f \n. m2:%f, m3:%f \n”, s2.rno, s2.name, s2.m1, s2.m2, s2.m3);printf(“ total :%f \n , average: %f \n”, s2.tot, s2.avg);}

Page 12: Unit4 (2)

Array of Structures:

Perhaps the most common usage of structures is an array of structures.

->To declare an array of structures you must first define a structure and then declare an array variable of that type.

For example to declare a 100 element array of structures of type “student” write

struct student

{

int rno;

int m1,m2,m3;

int tot,avg;

}

struct student s[100];-Structure Variable Declaration

Page 13: Unit4 (2)

DEMONSTRATING ARRAY OF STRUCTURES:

#include<stdio.h>

#include <conio.h>

void main()

{

/*1.Defining an array of structures*/

struct student

{

int rno;

int m1,m2,m3;

int tot,avg;

};

Page 14: Unit4 (2)

/*2.Creating an array of Structures*/

struct student s[100];

int i,n;

clrscr();

printf(“Enter n value\n”);

/*3.Read total no.of students to n*/

scanf(“%d”,&n);

/*4.Reading student details*/

for(i=0;i<n;i++)

{

printf(“Enter details of %d student\n”,i+1);

scanf(“%d”,&s[i].rno);

Page 15: Unit4 (2)

scanf(“%d%d%d”,&s[i].m1,&s[i].m1,&s[i].m2,&s[i].m3);

/*5.Calculate tot,avg marks */

for(i=0;i<n;i++)

{

s[i].tot=s[i].m1+s[i].m2+s[i].m3;

s[i].avg=s[i].tot/3;

}

/*6.Display the student details*/

for(i=0;i<n;i++)

{

printf(“The following are the %d student details\n”,i+1);

printf(“Rollno:%d\n”,s[i].rno);

Page 16: Unit4 (2)

printf(“M1:%d\n”,s[i].m1);

printf(“M2:%d\n”,s[i].m2);

printf(“M3:%d\n”,s[i].m3);

printf(“Total:%d\n”,s[i].tot);

printf(“Average:%d\n”,s[i].avg);

}

}

Page 17: Unit4 (2)

Arrays with in Structures:

A member of a structure may be either a simple or compound type.A simple member is one that is of any of the built-in datatypes such as integer or character.

->The compound datatypes include one dimensional and multidimensional arrays of other datatypes and structures.

For example consider this Structure:

struct x

{

int rno;

int m[3];/* An Array is described as a structure member*/

int tot,avg;

}

Page 18: Unit4 (2)

Nested Structures:

when a structure variable is a member of another structure, it is called a Nested structure.

->In the below example structure variable ‘z’ is declared as structure

Member to another structure.EX:struct Test { int a; int b;};Struct Exam{ int x; int y; struct Test z;};

Page 19: Unit4 (2)

Structures and Function:

A Structure can be passed to a function as a one variable or as an individual member. The scope of a structure declaration should be external storage class whenever a function in the main().

Program Using a Structure datatype:

#include<stdio.h>

/*Defining a Structure*/

struct Test

{

int a;

int b;

};

/*Prototype*/

Page 20: Unit4 (2)

struct Test Modify(struct Test s2);

void main()

{

struct Test T1={10,20};

T1=Modify(T1);

printf(“After calling modify\n”);

printf(“T1.a=%d\n”,T1.a);

printf(“T1.b=%d\n”,T1.b);

}

Stuct Test Modify(struct Test s2)

{

s2.a=s2.a+10;

Page 21: Unit4 (2)

S2.b=s2.b+10;

return s2;

}

10

a

10

b

20

3020

b

20

20+10

=30

a10

10+10=20

Page 22: Unit4 (2)

Union:

Union is another datatype with two or more members,similar to structure.But in this case all the members share a common memory location.The members of a union can be refered by using dot operator as in the case of structure.

The size of union can contain only one type of member at any one time.

The size of union corresponds to the length of the longest member.

Syntax:

Union Userdefined_datatype

{

datatype member 1;

datatype member n;};

Page 23: Unit4 (2)

Union Test

{

char a;

int b;

float c;

};

We may have structures with unions and unions with in structures. Unions may also be intialized like structures.However,Since only one member can be active at a time,Usually the assigned value will go to the 1st member of union.