Title: Aggregate data structures: structs As well as more on arrays
1Aggregate data structuresstructsAs well as
more on arrays
Reference Hanly and Koffman, Ch 7, 8, 11.1-5
2Post-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
4The picture so far simple types
- simple data types
- int, char, float.....
- pointers to simple data types
- int , char , float
5Simple data types
6
char
6
int
6.0
double
666666
char
666666
int
666666
double
6The picture so far array types
- Arrays of elements of the same simple type
- int, char, int, char .
7Array 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)
8Array 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
10How 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
11Enumerated types
- Another simple type
- Maps to int
- Associates names with values
- You saw it in the lecture on style
- It is here for completeness
12Enumerated types
- enum day_name
-
- Sun, Mon, Tue, Wed, Thu, Fri, Sat, day_undef
-
- Maps to integers, 0 .. 7
- Sun
- So very close to int
13Enumerated types
- enum month_name
-
- Jan, Feb, Mar, Apr, May, Jun,
- Jul, Aug, Sep, Oct, Nov, Dec,
- month_undef
-
14Recall 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)
15Now - 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)
16Now - 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)
17Memory review so far enums Heterogenous data
structures - struct typedef Sizes in
memory Homogenous data structures - arrays
18Structure definition
- struct date
-
- enum day_name day
- int day_num
- enum month_name month
- int year
19Structure 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
21Another structure definition
- struct car_desc
-
- enum car_cols colour
- enum car_make make
- int year
22General 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
24record/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
26Accessing 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
28Array 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)
29Array 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)
30Array 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)
31Remember 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
32When you need to know the size of things
33Accessing 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
34Exercise
35Exercise
- 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
38Examples 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)
39General form
- type array-namesize
- value, ...
40Remember 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)
42On out of bounds arrays
43On 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
- ...
45Multi-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
48Post-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
49Sources
- 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