Download presentation
Presentation is loading. Please wait.
Published byPrudence Davidson Modified over 9 years ago
1
5/3/01 Sudeshna Sarkar, CSE, IIT Kharagpur1 Structures Lecture 21 5.3.2001.
2
5/3/01 Sudeshna Sarkar, CSE, IIT Kharagpur2 Heterogeneous Structures Collection of values of possibly different types. Name the collection. Name the components. Example : Student record Singhal name "V Singhal" rollno "00CS1001" classtest 14 midterm 78 final 73 grade ‘B
3
5/3/01 Sudeshna Sarkar, CSE, IIT Kharagpur3 Structure : terminology A struct is a group of items (variables) which may be of different types. Each item is identified by its own identifier, each of which is known as a member or field of the structure. A struct is sometimes called a record or structure. Structs are the basis of classes in C++ and Java.
4
5/3/01 Sudeshna Sarkar, CSE, IIT Kharagpur4 Structure declaration struct { char first[10]; char midinit; char last[20]; } sname, ename; This declaration creates two structure variables, sname and ename, each of which contains 3 members. We can use sname.first, ename.midinit, etc.
5
5/3/01 Sudeshna Sarkar, CSE, IIT Kharagpur5 Members To access the members of a structure, we use the member access operator “.”. strcpy (sname.first, “Sudeshna”); sname.midinit = ‘K’; strcpy (sname.last, “Sarkar”) ;
6
5/3/01 Sudeshna Sarkar, CSE, IIT Kharagpur6 Tagged structure struct nametype { char first[10]; char midinit; char last[20]; }; struct nametype sname, ename; typedef struct nametype NTYPE; NTYPE aname, bname; This definition creates a structure tag nametype containing 3 members: first, midinit, last. Variables may be declared of type struct. typedef is normally used to give names to a struct type.
7
5/3/01 Sudeshna Sarkar, CSE, IIT Kharagpur7 typedef typedef struct { char first[10]; char midinit; char last[20]; } NAMETYPE; NAMETYPE sname,ename;
8
5/3/01 Sudeshna Sarkar, CSE, IIT Kharagpur8 Another example #define MAX_NAME 40 typedef struct{ char name[MAX_NAME+1]; char rollno[10]; int classtest; int midterm; int final; char grade; } StudentRecord; Defines a new data type called StudentRecord. Does not declare a variable.
9
5/3/01 Sudeshna Sarkar, CSE, IIT Kharagpur9 Declaring struct variables /* typedef structs go at top of program */... int..... float.... StudentRecord s1; StudentRecord singhal ; /* StudentRecord is a type; s1 and singhal are variables*/ struct nametype aname; /* struct nametype is a type; aname is a variable */
10
5/3/01 Sudeshna Sarkar, CSE, IIT Kharagpur10 Things you can and can't do You can Use = to assign whole struct variables You can Have a struct as a function return type You cannot Use == to directly compare struct variables; can compare fields directly You cannot Directly scanf or printf structs; can read fields one by one.
11
5/3/01 Sudeshna Sarkar, CSE, IIT Kharagpur11 Struct initializers /* typedef structs go on top */ StudentRecord s1 = {"V Singhal", "00CS1002", 15, 78, 73, 'B'}; Using components of struct variables s1.classtest = 46; s1.midterm = 78; scanf ("%d", &s1.rollno) ;
12
5/3/01 Sudeshna Sarkar, CSE, IIT Kharagpur12 Assigning whole structs s1 = singhal; is equivalent to strcpy(s1.name, singhal.name) ; strcpy(s1.rollno, singhal.rollno; s1.classtest = singhal.classtest; s1.midterm = singhal.midterm; s1.final = singhal.final; s1.grade = singhal.grade;
13
5/3/01 Sudeshna Sarkar, CSE, IIT Kharagpur13 Within a given structure, the member names must be unique. However, members in different structures may have the same name. A member is always accessed through a structure identifier. struct fruit { char name[20]; int calories; }; struct vegetable { char name[30]; int calories; }; struct fruit mango; struct vegetable potato; It is clear that we can access mango.calories and potato.calories without any ambiguity.
14
5/3/01 Sudeshna Sarkar, CSE, IIT Kharagpur14 Complicated structures A member of a structure can be an array or another structure. struct grocerylist { struct fruit flist[10]; struct vegetable vlist[20]; } ; You can have an array of structures. struct card { int pips; char suit; } deck[52] ;
15
5/3/01 Sudeshna Sarkar, CSE, IIT Kharagpur15 A function using struct array int fail (StudentRecord slist []) { int i, cnt=0; for (i=0; i<CLASS_SIZE; i++) cnt += slist[i].grade == ‘F’; return cnt; }
16
5/3/01 Sudeshna Sarkar, CSE, IIT Kharagpur16 Using structures with functions Structures can be passed as arguments to functions. Structures can be returned from functions. Call by value is used if a structure is a function parameter, meaning that a local copy is made for use in the body of the function. If a member of the structure is an array, then the array gets copied as well. If the structure is large, passing the structure as an argument can be relatively inefficient. An address of th structure may be used as the parameter.
17
5/3/01 Sudeshna Sarkar, CSE, IIT Kharagpur17 Union A union is like a structure, except that the members of a union share the same space in memory. union int_or_float { int i; float f; }; It is the programmer’s responsibility to know which representation is currently stored in a union variable.
18
5/3/01 Sudeshna Sarkar, CSE, IIT Kharagpur18 Arrays of Structures A struct represents a single record. Typically structs are used to deal with collections of such records Examples : student records, employee records, book records,... In each case we will hav multiple instances of the struct type. Arrays of structs are the natural way to do this.
19
5/3/01 Sudeshna Sarkar, CSE, IIT Kharagpur19 Arrays of structs : declaration & use Each declaration below declares an array, where each array element is a structure: point corner_points[10] ; StudentRecord btech01[MAXS] ; We access a field of a struct in an array by specifying the array element and then the field : btech01[i].name corner_points[4].x
20
5/3/01 Sudeshna Sarkar, CSE, IIT Kharagpur20 Naming in struct Arrays point pentagon[5]; x y x y x y x y x y pentagon : an array of points pentagon[1] : a point structure pentagon[4].x : a double
21
5/3/01 Sudeshna Sarkar, CSE, IIT Kharagpur21 Using Arrays of structs StudentRecord class[MAXS];... for (i=0; i<nstudents; i++){ scanf (“%d%d”, &class[i].midterm, &class[i].final); class[i].grade = (double)(class[i].midterm+class[i].final)/50.0; }
22
5/3/01 Sudeshna Sarkar, CSE, IIT Kharagpur22 struct Array elements as parameters void draw_line (point p1, point p2) {... }... point pentagon[5];... for (i=0;i<4;i++) draw_line (pentagon[i], pentagon[i+1]); draw_line (pentagon[4], pentagon[0]);
23
5/3/01 Sudeshna Sarkar, CSE, IIT Kharagpur23 structs as Parameters A single struct is passed by value. all of its components are copied from the argument (actual parameter) to initialize the (formal) parameter. point set_midpt (point a, point b) {... } int main (void) { point p1, p2, m;... m = set_midpt(p1, p2); }
24
5/3/01 Sudeshna Sarkar, CSE, IIT Kharagpur24 Passing Arrays of structs An array of structs is an array. When any array is an argument (actual parameter), it is passed by reference, not copied [As for any array] The parameter is an alias of the actual array argument. int avg (StudentRec class[MAX]) {... } int main (void) { StudentRec bt01[MAX]; int average;... average = avg_midpt(bt01) ; }
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.