Post on 05-Jan-2016
5/3/01 Sudeshna Sarkar, CSE, IIT Kharagpur
1
Structures
Lecture 215.3.2001.
5/3/01 Sudeshna Sarkar, CSE, IIT Kharagpur
2
Heterogeneous Structures
Collection of values of possibly different types.
Name the collection.Name the components.
Example : Student recordSinghal
name "V Singhal"rollno "00CS1001"classtest 14midterm 78final 73grade ‘B
5/3/01 Sudeshna Sarkar, CSE, IIT Kharagpur
3
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.
5/3/01 Sudeshna Sarkar, CSE, IIT Kharagpur
4
Structure declaration
struct {char first[10];char midinit;char last[20];
} sname, ename;
This declaration createstwo structure variables,sname and ename, eachof which contains 3members.We can use sname.first,ename.midinit, etc.
5/3/01 Sudeshna Sarkar, CSE, IIT Kharagpur
5
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”) ;
5/3/01 Sudeshna Sarkar, CSE, IIT Kharagpur
6
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 createsa structure tag nametypecontaining 3 members:first, midinit, last.Variables may be declaredof type struct <tagname>.
typedef is normally usedto give names to a struct type.
5/3/01 Sudeshna Sarkar, CSE, IIT Kharagpur
7
typedeftypedef struct {
char first[10];char midinit;char last[20];
} NAMETYPE;NAMETYPE
sname,ename;
5/3/01 Sudeshna Sarkar, CSE, IIT Kharagpur
8
Another example#define MAX_NAME 40typedef 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.
5/3/01 Sudeshna Sarkar, CSE, IIT Kharagpur
9
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 */
5/3/01 Sudeshna Sarkar, CSE, IIT Kharagpur
10
Things you can and can't do
You canUse = 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.
5/3/01 Sudeshna Sarkar, CSE, IIT Kharagpur
11
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) ;
5/3/01 Sudeshna Sarkar, CSE, IIT Kharagpur
12
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;
5/3/01 Sudeshna Sarkar, CSE, IIT Kharagpur
13
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 andpotato.calories withoutany ambiguity.
5/3/01 Sudeshna Sarkar, CSE, IIT Kharagpur
14
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] ;
5/3/01 Sudeshna Sarkar, CSE, IIT Kharagpur
15
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;
}
5/3/01 Sudeshna Sarkar, CSE, IIT Kharagpur
16
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.
5/3/01 Sudeshna Sarkar, CSE, IIT Kharagpur
17
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.
5/3/01 Sudeshna Sarkar, CSE, IIT Kharagpur
18
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.
5/3/01 Sudeshna Sarkar, CSE, IIT Kharagpur
19
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].namecorner_points[4].x
5/3/01 Sudeshna Sarkar, CSE, IIT Kharagpur
20
Naming in struct Arrayspoint pentagon[5];
xy
xy
xy
xy
xy
pentagon : an array of points
pentagon[1] : a point structure
pentagon[4].x : a double
5/3/01 Sudeshna Sarkar, CSE, IIT Kharagpur
21
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;
}
5/3/01 Sudeshna Sarkar, CSE, IIT Kharagpur
22
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]);
5/3/01 Sudeshna Sarkar, CSE, IIT Kharagpur
23
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);
}
5/3/01 Sudeshna Sarkar, CSE, IIT Kharagpur
24
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) ;
}