Aggregate data structures: structs As well as more on arrays - PowerPoint PPT Presentation

1 / 49
About This Presentation
Title:

Aggregate data structures: structs As well as more on arrays

Description:

Homogenous data structures - arrays. School of Information Technologies ... Homogenous data structures - arrays. School of Information Technologies. typedef ... – PowerPoint PPT presentation

Number of Views:33
Avg rating:3.0/5.0
Slides: 50
Provided by: judy97
Category:

less

Transcript and Presenter's Notes

Title: Aggregate data structures: structs As well as more on arrays


1
Aggregate data structuresstructsAs well as
more on arrays
  • COMP5212
  • Week 4

Reference Hanly and Koffman, Ch 7, 8, 11.1-5
2
Post-conditions
  • You will be able to
  • use enums
  • use structures
  • typedefs
  • arrays (strings)
  • use pointers with them
  • draw pictures of these in memory

3
  • Memory review so far
  • enums
  • Heterogenous data structures - struct
  • typedef
  • Sizes in memory
  • Homogenous data structures - arrays

4
The picture so far simple types
  • simple data types
  • int, char, float.....
  • pointers to simple data types
  • int , char , float

5
Simple data types
6
char
6
int
6.0
double
666666
char
666666
int
666666
double
6
The picture so far array types
  • Arrays of elements of the same simple type
  • int, char, int, char .

7
Array of char
With arrays of char, each of the boxes in the
diagram below is really 1 byte. Adding 1 to
the pointer adds 1 byte.
ptr
A
2
7
m
l

y
(ptr1)
8
Array of int
With arrays of int, each of the boxes in the
diagram below is really 4 bytes. Adding 1 to
the pointer adds 4 bytes.
ptr
-1
234
17
12
53
92
725
(ptr1)
9
  • Memory review so far
  • enums
  • Heterogenous data structures - struct
  • typedef
  • Sizes in memory
  • Homogenous data structures - arrays

10
How to represent this?
  • One, two, three, four, five, six, seven, eight,
    nine, ten, eleven, twelve, thirteen, fourteen,
    fifteen, sixteen, seventeen, nineteen, twenty,
    twenty_one, twenty_two, twenty_three,
    twenty_four, twenty_five, twenty_six,
    twenty_seven, twenty_eight, twenty_nine, thirty,
    thirty_one, thirty_two, thirty_three,
    thirty_four, thirty_five, thirty_six, thirty_se

11
Enumerated types
  • Another simple type
  • Maps to int
  • Associates names with values
  • You saw it in the lecture on style
  • It is here for completeness

12
Enumerated types
  • enum day_name
  • Sun, Mon, Tue, Wed, Thu, Fri, Sat, day_undef
  • Maps to integers, 0 .. 7
  • Sun
  • So very close to int

13
Enumerated types
  • enum month_name
  • Jan, Feb, Mar, Apr, May, Jun,
  • Jul, Aug, Sep, Oct, Nov, Dec,
  • month_undef

14
Recall array of int
With arrays of int, each of the boxes in the
diagram below is really 4 bytes. Adding 1 to
the pointer adds 4 bytes.
ptr
-1
234
17
12
53
92
725
(ptr1)
15
Now - array of day_name
With arrays of int-enum, each of the boxes in the
diagram below is really 4 bytes. Adding 1 to
the pointer adds 4 bytes.
ptr
Sun
Sat
Sun
Mon
Wed
Sat
Thu
(ptr1)
16
Now - array of (int) day_name
With arrays of enum, each of the boxes in the
diagram below is really 4 bytes. Adding 1 to
the pointer adds 4 bytes.
ptr
0
6
0
1
3
6
5
(ptr1)
17
Memory review so far enums Heterogenous data
structures - struct typedef Sizes in
memory Homogenous data structures - arrays
18
Structure definition
  • struct date
  • enum day_name day
  • int day_num
  • enum month_name month
  • int year

19
Structure variables
  • struct date
  • enum day_name day
  • int day_num
  • enum month_name month
  • int year
  • struct date moonlanding
  • struct date deadline day_undef, 1, Jan,
    2000
  • struct date completion

20
  • struct date
  • enum day_name day
  • int day_num
  • enum month_name month
  • int year
  • struct date moonlanding
  • struct date deadline day_undef, 1,
    Jan, 2000
  • struct date completion

Structure definition
Structure initialisation
21
Another structure definition
  • struct car_desc
  • enum car_cols colour
  • enum car_make make
  • int year

22
General form
  • struct tag
  • member-declarations
  • identifier-list
  • Once tag is defined, can declare structs with
  • struct tag identifier-list

23
  • Memory review so far
  • enums
  • Heterogenous data structures - struct
  • typedef
  • Sizes in memory
  • Homogenous data structures - arrays

24
record/structure
  • typedef struct date
  • enum day_name day
  • int day_num
  • enum month_name month
  • int year
  • Date

members/fields of the record
25
  • typedef struct date
  • enum day_name day
  • int day_num
  • enum month_name month
  • int year
  • Date
  • Date Big_day Mon, 7, Jan, 1980
  • Date moonlanding
  • Date dopday day_undef, 1, Jan, 2000
  • Date completion

26
Accessing struct members/fields
  • Use . (the dot operator)
  • E.g.
  • printf(month of landing d\n,moonlanding.month
    )
  • If it is a pointer struct variable, use the
    indirection operators (-gt) or ( ).
  • completion-gtmonth
  • (completion).month

27
  • Memory review so far
  • enums
  • Heterogenous data structures - struct
  • typedef
  • Sizes in memory
  • Homogenous data structures - arrays

28
Array of char
With arrays of char, each of the boxes in the
diagram below is really 1 byte. Adding 1 to
the pointer adds 1 byte.
ptr
A
2
7
m
l

y
(ptr1)
29
Array of int
With arrays of int, each of the boxes in the
diagram below is really 4 bytes. Adding 1 to
the pointer adds 4 bytes.
ptr
-1
234
17
12
53
92
725
(ptr1)
30
Array of THING
With arrays of THING, each of the boxes in the
diagram below is sizeof(THING) bytes. Adding 1
to the pointer adds sizeof(THING) bytes.
str
jkjkjjjjjkjkj
jkjkjjjjjkjkj
jkjkjjjjjkjkj
jkjkjjjjjkjkj
jkjkjjjjjkjkj
jkjkjjjjjkjkj
jkjkjjjjjkjkj
(str1)
31
Remember Pointer Scalars
This works because of a special (hidden) value
associated with a pointer at the time of
declaration - the pointers scalar value. It
is used in arithmetic operations to ensure that
the expected behaviour occurs.
Typical scalar value is 1
char msg int ptr short fred
Typical scalar value is 4
Typical scalar value is 2
32
When you need to know the size of things
33
Accessing the size of data elements
  • sizeof operator
  • sizeof(int)
  • sizeof(char)
  • sizeof(Date)
  • Why would you want to know sizeof?
  • soon to be used with malloc

34
Exercise
35
Exercise
  • Given the declarations
  • struct A
  • int a
  • int b
  • int c10
  • struct A x
  • struct A p
  • What does this look like in memory?
  • What do these do?

36
  • struct A
  • int a
  • int b
  • int c10
  • struct A x
  • struct A p
  • What do these do?
  • x.a
  • (p).a
  • (p-gtb)
  • p -gt c0
  • x p

37
  • Memory review so far
  • Enums
  • Heterogenous data structures - struct
  • Typedef
  • Sizes in memory
  • Homogenous data structures - arrays

38
Examples of initialisations
  • int A10 1, 1, 1, 1, 1, 1, 1, 1, 1, 1
  • int B10 0, 0, 0, 0, 0, 0, 0, 0, 0, 0
  • int B10 0
  • int B10
  • int C 1, 2, 3, 4, 5, 6
  • int C_size sizeof C / sizeof (int)

39
General form
  • type array-namesize
  • value, ...

40
Remember array-pointeryi equivalent to (yi)
  • Consider these declarations.
  • int AN
  • int pa A0
  • struct date BN
  • struct date pb B0
  • Draw the memory model.

41
  • int AN
  • int pa A0
  • struct date BN
  • struct date pb B0
  • A0
  • pa
  • A
  • Aj
  • (pa j)
  • (A j)
  • Bj
  • (pb j)
  • (B j)

42
On out of bounds arrays
  • Defensive programming.

43
On out of bounds arrays
  • No checking!
  • No help from the compiler!
  • int A100
  • ...
  • An 77
  • ...

44
  • / contains relevant macro definition
  • /
  • include ltassert.hgt
  • ...
  • int A100
  • ...
  • assert((n gt 0) (n lt 100))
  • An 77
  • ...

45
Multi-dimensional arrays
  • Freedom to see things flexibly

46
  • int A35
  • 1, 0, 0, 0, 1 ,
  • 1, 1, 1, 1, 1 ,
  • 0, 0, 1, 0, 1

47
  • void sum()
  • int i
  • int j
  • for (i 0 i lt 3 i)
  • for (j 0 j lt 5 j)
  • APLUSBij Aij Bij

48
Post-conditions (so far)
  • You will be able to
  • use enums
  • use structures
  • typedefs
  • arrays (strings)
  • use pointers with them
  • draw pictures of these in memory

49
Sources
  • All images (in order) from
  • Microsoft Word clipart
  • http//www.10-7.com/humor/photopages/
  • sign.jpg
  • keepright.jpg
  • one ways.jpg
  • killerchurch.jpg
  • marquee.jpg
  • they are assholes.jpg
  • wendys.jpg
  • santa plane.jpg
  • Lecture prepared by Judy Kay Selvakennedy
Write a Comment
User Comments (0)
About PowerShow.com