Bits and Bytes - PowerPoint PPT Presentation

About This Presentation
Title:

Bits and Bytes

Description:

Structured Data Objects (Structs) Chapter 7 – PowerPoint PPT presentation

Number of Views:143
Avg rating:3.0/5.0
Slides: 24
Provided by: Peet150
Learn more at: http://pkirs.utep.edu
Category:
Tags: bits | bytes

less

Transcript and Presenter's Notes

Title: Bits and Bytes


1
Structured Data Objects (Structs)
Chapter 7
2
Structs (structured data objects)
Recall our definition of an array an array
is a fixed number of contiguous storage elements
all of the same data type. The abstract
data type struct avoids one restriction
The elements be NEED NOT all be of the same
data type.
A struct Consists of 2 or more component
data types A component part may be either a
simple data type
(e.g., int, unsigned int, float, char, long,
double, etc.)
A pointer
(e.g., int, float, char, long, double,
etc.)
Or another structured object
(i.e., abstract type struct)
3
A struct corresponds to records in a database
Assume we maintain a simple database of
student information
Attribute (Field)
Data Type
Student Name
String (Allow for 30 characters)
Student ID
Integer (int)
Major
String (Allow for 10 characters)
Grade-Point Average
Real (float)
Hours Completed
Integer (int)
This record (lets call it student)
Consists of 5 (five) data elements
(Student Name, Student ID, Major, Grade-Point
Average, Hours Completed)
Consists of 3 (three) different data types
(Strings (2), Integers (2), and a real (float))
4
Declaring Records (in other languages)
COBOL 01 STUDENT 02
STUDENT-NAME PIC X(30). 02
STUDENT-ID PIC 9(6). 02 MAJOR PIC
X(10). 02 GPA PIC 9V99. 02
TOTAL-HOURS PIC 999.
Pascal type student record
student_name array1..30 of char
student_id integer major
array1..10 of char gpa real.
total_hours integer
end
SQL create table student (
student_name char(30) not
null, student_id integer
not null, major
char(10) default CIS,
gpa decimal(4,2),
total_hours smallint,
primary key
(student_id),
check (gpa gt 0)
)
5
Declaring Records (in C)
struct student char student_name31 //
Remember we need 1-byte for \0 int
student_id char major11 //
Remember we need 1-byte for \0 float
gpa int total_hrs
We have created a new data type struct student
just as with basic data types, we can now
associate variables (specific locations in RAM)
with the data type struct student just as
with basic data types, whenever we declare a
variable to be of type struct student, we are
requesting a specific number of contiguous bytes
of RAM storage.
How many bytes of storage do we need for a struct?
That depends on the struct
6
For our struct (struct student)
Element
Bytes
char student_name31
31
int student_id
2
char major11
11
float gpa
4
int total_hrs
2
50
We need 50 bytes of contiguous RAM storage for
our data type struct student
How do we declare and use structs in a C program
??
7
Consider the following program
include ltstdio.hgt
struct student char student_name31
int student_id char major11 float
gpa int total_hrs int
main() struct student active
"Favre, Brett",12345,"CIS",3.27,67 struct
student alumni "White, Reggie", 23456,
"Pain", 1.34, 89 printf("Name 13s
14s\n",active.student_name,
alumni.student_name) printf("ID
13d 14d\n",active.student_id,
alumni.student_id)
printf("Major 13s 14s\n",active.major,
alumni.major)
printf("gpa 13.3f 14.3f\n",active.gpa,
alumni.gpa)
printf("Hours 13d 14d\n",active.total_hrs,
alumni.total_hrs)
return 0
8
How is this data stored in RAM??
As with ALL data types, the data type struct has
a base address assume that variable active
(of data type struct student) has the base
address 7520 (and requiring 50 contiguous bytes,
to 7569)
9
How can the address of each element in the struct
be calculated??
Just as we calculated the address of each element
in an array, the address of each element in a
struct is determined by adding the amount of
storage required for each element and adding it
to the base address.
Element
Start addr.
Storage
Next Address
Type
student_name
7520
31-bytes
7520 31 7551
char 31
student_ID
7551
2-bytes
7551 2 7553
int
Or 7520 33 7553
major
7553
11-bytes
7553 11 7564
char 11
Or 7520 44 7564
float
gpa
7564
4-bytes
7564 4 7568
Or 7520 48 7568
total_hrs
int
7568
2-bytes
N/A
10
BUT, if structs are equivalent to records in a
database, how can a database consist of only one
(1) record ???
We can have an array of struct, just as we can
have an array of int, and array of float, an
array of char (string), etc. Remember a
struct, while abstract, is simply a data type
For the sake of simplicity, lets assume the
following record structure
struct student char student_name20
// names no longer than 19 characters float
gpa
How would this struct be set up as an array ???
11
Consider the following code
include ltstdio.hgt struct student char
student_name20 float gpa int main()
struct student active5 "Favre,
Brett",3.26, "White, Reggie",1.34,
"Smith, Bruce",3.78, "Smith,
Emmitt", 2.05, "Rice, Jerry", 3.55
int i for (i 0 i lt 5 i)
printf("Name 15s gpa 4.2f\n",
activei.student_name,activei.gpa) return
0
12
The program, as entered, produces the output
Name Favre, Brett gpa 3.26 Name White,
Reggie gpa 1.34 Name Smith, Bruce gpa
3.78 Name Smith, Emmitt gpa 2.05 Name
Rice, Jerry gpa 3.55
How does this program work ?? What if we were
searching for a specific name, . Say, Bruce
Smith ???
Consider the following C code
13
include ltstdio.hgt
include ltstring.hgt struct student char
student_name20 float gpa int
main() struct student active5 "Favre,
Brett",3.26, "White, Reggie",1.34,
"Smith, Bruce",3.78, "Smith,
Emmitt",2.05, "Rice, Jerry",3.55 int i
0 char find20 "Smith, Bruce"
while((strcmp(activei.student_name,find)!0)(i
lt5)) i if (i gt 5) printf("The name
is NOT on the list") else printf("s IS on
the list (gpa 4.2f)",
activei.student_name, activei.gpa) return
0
14
structs and pointers
structs, like any other data type, have base
addresses, and can be accessed by referring to
its address. Consider the following modification
of our previous program
include ltstdio.hgt
struct student char name18
float gpa int main() struct student
active5 "Favre, Brett",3.26, "White,
Reggie",1.34, "Smith, Bruce",3.78, "Smith,
Emmitt",2.05, "Rice, Jerry",3.55 struct
student record active printf(lu\n,
record) while(record lt active4)
printf(lu 20s 7.3f\n, record, record-gtname,

record-gtgpa) record return 0
15
The following output would be produced
86712 86562Favre, Brett3.260 86584
White, Reggie1.340 86606Smith,
Bruce3.780 86628Smith,
Emmitt2.050 86650Rice, Jerry3.550
Note These addresses are not the TRUE
addresses, and they may change with each run
How does this program work ??
16
Lets take it from the line
struct student record active
The BASE address of our array active
Our new data type
pointer to
The same as active0
struct student char name18 float gpa

Meaning, the variable pointer record is now
initialized to the BASE address of array active (
or, in or case, 86562)
How would this appear in RAM??
17
The contents of address record ( active OR
active0)
Points to
Located at
Continue to
18
The next statement in the program
printf(lu\n, record)
Prints the BASE ADDRESS of our structure
NOT the contents
(Which contains the base address of our array
active)
Notice also that ALL pointers (e.g., record)
require 4-bytes of storage
We can now begin our loop
record CONTAINS the address
while(record lt active4)
Which is less than the base address of our 5th
record (86650)
Enter Loop
19
Now we simply print out the first record
printf(lu 20s 7.3f\n, record, record-gtname,
record-gtgpa)
The CONTENTS of record
Field name (on 20 spaces)
Field gpa (as f7.3)
86562
Favre, Brett
3.260
Notice that the statement record-gtname is a
REDIRECT
Go to the ADDRESS contained in record and
find field name
i.e., record contains the address 86562, and
name is at 86562
The redirect record-gtgpa would yield the
address 86580
An alternative statement is (record).name
20
The next statement
record
Increments the contents of record
Changed to
The contents of record are increased by 22
Why are the contents increased by 22 and not 1 ???
Record is pointer to our data type struct student
(Initialized as struct student record) which
requires 22-bytes of storage. Incrementing
increases the value contained by 22. Decrementing
it (i.e., record -- or --record would
decrease the value contained by 22)
21
Remember
A pointer is an address in RAM which
contains an address What type of data is at
that address MUST be known in advance
int integer_pointer // contains an address to
an integer float float_pointer // contains an
address to a float double double_pointer //
contains an address to a double
IF
Variable
Located at
Contains
integer_pointer
12345
46788
float_pointer
12349
22734
double_pointer
12353
71122
Then
Statement
Will result in the new value
integer_pointer
46790
float_pointer
22738
double_pointer--
71114
22
For the rest of the loop
record value
Output
86584
86584White, Reggie1.340
86606Smith, Bruce3.780
86606
86628Smith, Emmitt2.050
86628
86650Rice, Jerry3.550
86650
86672
Greater than
Stop
23
(No Transcript)
Write a Comment
User Comments (0)
About PowerShow.com