Unit4 (2)

455 views

Published on

Published in: Technology
0 Comments
0 Likes
Statistics
Notes
  • Be the first to comment

  • Be the first to like this

No Downloads
Views
Total views
455
On SlideShare
0
From Embeds
0
Number of Embeds
3
Actions
Shares
0
Downloads
12
Comments
0
Likes
0
Embeds 0
No embeds

No notes for slide

Unit4 (2)

  1. 1. 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 rno gender 2 1 { int rno; age ht wt 2 4 4 char gender; int age; 13 bytes of memory is allocated in s1
  2. 2. 13 bytes of memory is allocated to s2 float ht; age gender float wt; 2 1 }; age ht wt 2 4 4 struct student s1,s2; }When all these data items are grouped under on single variablename, 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 structuresare named as records
  3. 3. DEFINING A STRUCTURE:syntax: Example: struct user defined datatype struct student { { int rno; datatype member1; int age; char gender; datatype member2; float ht; ……………. float wt; }; ………… datatype membern: };
  4. 4. In the given syntax struct is keyword used to create new user defineddata type.An user defined data type name may be any valid ‘c’ identifier ( userdefined 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 givenexample 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 curlybraces.The structure definition should be terminated with semicolon.
  5. 5. DECLARING STRUCTURE VARIABLE: Once a new user defined datatype is created by using thekeyboard struct then we can create any no. of structure variables of thattype.syntax: struct user defined_data type name structure var1, structure var2,……….. Struct varn;Above statement is called declarative statement of structure variablesand it allocates memory space to these variables.We can also declare structure variables immediately after structuredefinition.
  6. 6. ACCESSING STRUCTURE MEMBERS: . Structure member accessing operator  S1.gender=‘m’; s1.age=25;Structure S1.rno=100 S1.ht=5.5; s1.wt=60.2;variable Structure member •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.
  7. 7. syntax: Ex: s1.rno, s1.age, s1.gender, s1.ht, s1.wt S1.rno=100; s1.gender=M; s1.age=25; s1.ht=5.5; s1.wt=60.2 s1 s2 rno gender rno gender 100 M age ht wt age ht wt 25 5.5 60.2ASSIGNING VALUES TO STRUCTURE MEMBERS:By using the assignment operator(=) we can assign values to allstructure members of a structure variable. Syntax: structure variable.member = value; Ex: s1.rno= 100; s1.age=25
  8. 8. INITIALIZING A STRUCTURE VARIABLE:Structures can be initialized using initialize lists as with arrays. Toinitialize a structure follow the variable name in the structuredeclaration with an equal sign and brace enclosed comma separatedlist 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. Itwill 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);
  9. 9. PROGRAM USING STRUCTURES: Reads the details of 2 students and calculate total and averagemarks 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; };
  10. 10. /* 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;
  11. 11. /* 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:%dn, 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:%dn, 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);}
  12. 12. Array of Structures: Perhaps the most common usage of structures is an array ofstructures.->To declare an array of structures you must first define a structureand then declare an array variable of that type.For example to declare a 100 element array of structures of type“student” writestruct student{ int rno; int m1,m2,m3; int tot,avg;}struct student s[100];-Structure Variable Declaration
  13. 13. 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; };
  14. 14. /*2.Creating an array of Structures*/struct student s[100];int i,n;clrscr();printf(“Enter n valuen”);/*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 studentn”,i+1); scanf(“%d”,&s[i].rno);
  15. 15. 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 detailsn”,i+1); printf(“Rollno:%dn”,s[i].rno);
  16. 16. printf(“M1:%dn”,s[i].m1);printf(“M2:%dn”,s[i].m2);printf(“M3:%dn”,s[i].m3);printf(“Total:%dn”,s[i].tot);printf(“Average:%dn”,s[i].avg);}}
  17. 17. Arrays with in Structures: A member of a structure may be either a simple or compound type.simple member is one that is of any of the built-in datatypes such as integer orcharacter.->The compound datatypes include one dimensional and multidimensionalarrays 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;}
  18. 18. Nested Structures: when a structure variable is a member of another structure, it iscalled a Nested structure.->In the below example structure variable ‘z’ is declared as structureMember to another structure.EX:struct Test { int a; int b;};Struct Exam{ int x; int y; struct Test z;};
  19. 19. Structures and Function: A Structure can be passed to a function as a one variable oras an individual member. The scope of a structure declaration shouldbe 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*/
  20. 20. struct Test Modify(struct Test s2);void main(){ struct Test T1={10,20}; T1=Modify(T1); printf(“After calling modifyn”); printf(“T1.a=%dn”,T1.a); printf(“T1.b=%dn”,T1.b);}Stuct Test Modify(struct Test s2){ s2.a=s2.a+10;
  21. 21. S2.b=s2.b+10;return s2;} a b a b 10 20 10 20 1 10+10 20+10 200 30 =20 =30
  22. 22. Union:Union is another datatype with two or more members,similar tostructure.But in this case all the members share a commonmemory location.The members of a union can be refered by usingdot operator as in the case of structure.The size of union can contain only one type of member at anyone time.The size of union corresponds to the length of the longestmember.Syntax:Union Userdefined_datatype{ datatype member 1; datatype member n;};
  23. 23. 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 onlyone member can be active at a time,Usually the assigned value willgo to the 1st member of union.

×