Title: 125 COMPLEX STRUCTURES
112-5 COMPLEX STRUCTURES
- Structures can contain
- Strings
- Arrays
- Other structures
These slides are intended for students at IUPUI
who are users of the book B.A. Forouzan, R.F.
Gilberg, Computer Science A Structured Approach
Using C, Brooks/Cole, second edition, 2001, ISBN
0-534-37482-4. Additions have been made to
existing slides from Brooks/Cole and some new
slides have been added by Kris A. Dines, Ph.D.,
Indiana University-Purdue University (IUPUI)
(kdines_at_iupui.edu) for a C course in Electrical
Engineering. Please see Brooks/Cole website
(www.brookscole.com) to download original slides.
2Structures with Arrays Inside
- typedef struct
-
- char name32
- int idNum
- double gpa
- Student
- //.
- Student s1
- s1.name0J
- s1.name1i
- s1.name2m
- s1.name3\0
- //..
- typedef struct
-
- char name
- //etc
- Student
- //
- Student s1
- // dynamically allocated string (array)
- s1.name(char)malloc(32sizeof(char))
- s1.name0J
- s1.name1i // s1.((name1))i
- s1.name2m
- s1.name3\0
3Shallow Copy vs. Deep Copy
- Student s1
- Student s2
- s2s1 // shallow copy
- typedef struct
-
- char name32
- int idNum
- double gpa
- Student
- typedef struct
-
- char name32
- int idNum
- double gpa
- Student
the pointer, name, is copied, but not contents of
array.
Shared
name is copied?same address in both
Assignment of structures results in a shallow
copy Pointers in the structure? memory they point
to is same
4Shallow Copy vs. Deep Copy
- // Using a function to copy
- void StudentCopy(Point s1, Point s2)
-
- int i
- s2-gtidNums1.idNum
- s2-gtgpas1.gpa
- i0
- while(s1.namei!\0)
-
- s2-gtnameis1.namei
- i
-
Need pointer to struct to affect where its data
lives
Passed by value means a copy is passed including
copy of pointer name
5Object-Oriented Philosophy
- C struct or typdef is a like a Class
- Class contains data and functions that operate
upon it (using it). In C, we just haved the data - Declaration gives an instance of the class
object - Student s1 // classStudent
- // instance s1 (an object)
6Object-Oriented Conventions C
- Student.h contains typedef prototypes of
functions in Student.c -
- Student.c contains functions that operate upon
student objects - ? an instance of Student is the first argument
- Student.c includes Student.h
- Main Programs using Student functions and/or data
types include Student.h
7File Layout
Every function in Student.c has Student as prefix
of name. ( or Student_ )
- // Student.c
- include Student.c
- void StudentCopy(Point s1,
- Point s2)
-
- int i
- s2-gtidNums1.idNum
- s2-gtgpas1.gpa
- i0
- while(s1.namei!\0)
-
- s2-gtnameis1.namei
- i
-
-
- // Student.h
- ifndef STUDENT_H
- define STUDENT_H
- typedef struct
-
- char name32
- int idNum
- double gpa
- Student
- void StudentCopy(Student s1,
- Student s2)
- endif
- // main
- include Student.h
8Data EncapsulationHiding the implementation
- Data members of Student are accessed only via
functions (get/set) Never directly except within
Student.c - void Student_SetGPA(Student s, double gpa)
- s-gtgpagpa
- double Student_GetGPA(Student s)
- return s-gtgpa
- void Student_SetName(Student s, char name)
- s-gtnamename
- char Student_GetName(Student s)
- return s-gtname
9Constructor/Destructor
- // constructor (in file, Student.c)
- Student Student_New(int idNum, double gpa, char
name) -
- Student result
- result(Student )malloc(sizeof(Student)) //
create a Student - result-gtidNumidNum
- result-gtgpagpa
- result-gtnamename
- return result
10Constructor/Destructor
- void Student_Delete(Student s)
-
- free(s)
11Structures and Functions
- Pass the structure type if you just need a copy
of its data members - Pass a pointer to the structure type if you want
to permanently change its data members as seen by
the calling function.
12Figure 12-18 Arrays Inside of Structures
13Array of Structures
- int numStudents1024
- Student eceStudentsnumStudents // all ece
students - eceStudents37.idNum44
- eceStudents37.gpa4.0
- eceStudents37.name0J
- //etc....
14Figure 12-20
15Figure 12-23
16Some Tips on Structures
- Adhere to object-oriented design style
- Usually best to pass pointers to structures as
fucntion args avoids copying the data - Usually best to return pointer to structure if
created inside a function - e.g., result of multiplying 2 Fractions
17Union
- Memory overlay
- Same memory used for different data types at
different times. - Only one type at a time
18Union Example
- typedef union
-
- int ivalue // 4-bytes
- double dvalue // 8-bytes
- IntDouble
- // enough room will be allocated to hold longest
data type - IntDouble a
- a.ivalue3 // store as an int
- int ba.ivalue // OK
- a.dvalue3.14
- int ca.dvalue // NOT OK data is a double
19Figure 12-24
20Figure 12-25