125 COMPLEX STRUCTURES - PowerPoint PPT Presentation

1 / 20
About This Presentation
Title:

125 COMPLEX STRUCTURES

Description:

Shallow Copy vs. Deep Copy. Student s1; Student s2; s2=s1; // shallow copy. typedef struct ... Usually best to return pointer to structure if created inside a function ... – PowerPoint PPT presentation

Number of Views:46
Avg rating:3.0/5.0
Slides: 21
Provided by: krisd9
Category:

less

Transcript and Presenter's Notes

Title: 125 COMPLEX STRUCTURES


1
12-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.
2
Structures 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

3
Shallow 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
4
Shallow 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
5
Object-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)

6
Object-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

7
File 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

8
Data 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

9
Constructor/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

10
Constructor/Destructor
  • void Student_Delete(Student s)
  • free(s)

11
Structures 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.

12
Figure 12-18 Arrays Inside of Structures
13
Array of Structures
  • int numStudents1024
  • Student eceStudentsnumStudents // all ece
    students
  • eceStudents37.idNum44
  • eceStudents37.gpa4.0
  • eceStudents37.name0J
  • //etc....

14
Figure 12-20
15
Figure 12-23
16
Some 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

17
Union
  • Memory overlay
  • Same memory used for different data types at
    different times.
  • Only one type at a time

18
Union 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

19
Figure 12-24
20
Figure 12-25
Write a Comment
User Comments (0)
About PowerShow.com