Structures and Enumerated Types - UNBaubanel/cs2023/structures.pdf · 2004-03-18 · Outcomes:...

31
Structures and Enumerated Types CS2023 Winter 2004

Transcript of Structures and Enumerated Types - UNBaubanel/cs2023/structures.pdf · 2004-03-18 · Outcomes:...

Page 1: Structures and Enumerated Types - UNBaubanel/cs2023/structures.pdf · 2004-03-18 · Outcomes: Structures C for Java Programmers, Chapters 11 (11.1 to 11.4) and 12 (12.1 to 12.2),

Structures and Enumerated Types

CS2023 Winter 2004

Page 2: Structures and Enumerated Types - UNBaubanel/cs2023/structures.pdf · 2004-03-18 · Outcomes: Structures C for Java Programmers, Chapters 11 (11.1 to 11.4) and 12 (12.1 to 12.2),

Outcomes: Structures

� C for Java Programmers, Chapters 11 (11.1 to 11.4) and 12 (12.1 to 12.2),

� After the conclusion of this section you should be able to

� Declare, intialize, and use structures and pointers to structures

� Efficiently pass structures to functions

� Combine structures and arrays to create arrays of structures and structures containing arrays

� Use enumerated types when you need ordered collections of named constants

Page 3: Structures and Enumerated Types - UNBaubanel/cs2023/structures.pdf · 2004-03-18 · Outcomes: Structures C for Java Programmers, Chapters 11 (11.1 to 11.4) and 12 (12.1 to 12.2),

Structures

Structures are user defined data types, which represent heterogeneous collections of data.

� � �� � � �� ��

� �� � � � � � �� �� �� � ���

� �� � � � � � �� � � �� � ��

�� � � � � �

��� � �� � � �� � �� � �� �

�� � ��

Page 4: Structures and Enumerated Types - UNBaubanel/cs2023/structures.pdf · 2004-03-18 · Outcomes: Structures C for Java Programmers, Chapters 11 (11.1 to 11.4) and 12 (12.1 to 12.2),

Structures

� �! " # � $% &�' (

# )* & $ � � +,* -. /0 1 2�3

# )* 4 * � � +* - . / 0 1 2�3

$ % � * 5 . 3

6 $7 8 $0 3

�:9 ; . < . & � � " # � $ % &' ( = # * % > . ' - $ � � . < =

# )* & $ � � +,* -. /0 1 2�3

# )* 4 * � � +* - . / 0 1 2�3

$ % � * 5 . 3

6 ? % &' @3

Structure tag

Page 5: Structures and Enumerated Types - UNBaubanel/cs2023/structures.pdf · 2004-03-18 · Outcomes: Structures C for Java Programmers, Chapters 11 (11.1 to 11.4) and 12 (12.1 to 12.2),

Using Structures

A:B CD ED F G A!H I J A KL F�M N

J OP H F KH G A QP RD ST U V�W

J OP H X P G A QP RD ST U VW

K L A P YD W

Z [ L F�M \W

[ L F�M \ C ]W

In order to access members of a structure:

C ]^ P YD _ ] `W

CH K L A Fa bc G L bd C ]^ F KH G A QP RD eW

Page 6: Structures and Enumerated Types - UNBaubanel/cs2023/structures.pdf · 2004-03-18 · Outcomes: Structures C for Java Programmers, Chapters 11 (11.1 to 11.4) and 12 (12.1 to 12.2),

Structure Errors

f g!h i j g kl mn o p k q r r r s�t

k l mn o p k kt

f g!h i j g kl mn o p k kt

f g!h i j g kl mn o p k q r r r s u vw t u

Page 7: Structures and Enumerated Types - UNBaubanel/cs2023/structures.pdf · 2004-03-18 · Outcomes: Structures C for Java Programmers, Chapters 11 (11.1 to 11.4) and 12 (12.1 to 12.2),

Nested Structures

x:y z{ |{ } ~ x!� � � x �

� �� � }� � ~ x �� �{ �� � ���

� �� � � � ~ x �� �{ �� � ��

� � x � �{ �

� � � }�� ��

xy z { |{ } ~ x� � � x �

� � }�� � � � } � �

| � � � �{ ~ � � � � y �

� � � z � � y { { ��

� � z � � y { { � { ��

{ �� � � }�� � � �{ � � ��

Page 8: Structures and Enumerated Types - UNBaubanel/cs2023/structures.pdf · 2004-03-18 · Outcomes: Structures C for Java Programmers, Chapters 11 (11.1 to 11.4) and 12 (12.1 to 12.2),

Assignments & Comparing Structures

�,� ��� � ��   �¡ ¢

�� £ � ¡ ¢

¤ ¥!¦ § ¨ ¥ © �� ¥ ª « � ¬ ­® ª�   ª ¡ ¢

ª� £ ª ¡ ¯ °± ² ª °   ¤ �� ¨ ± ª�   ª ¡ ª¦ ±

¤ ¥ ¦ § ¨ ¥ §¦ ± ¤ ¯

�� £ £ � ¡

¤ ¥!¦ ¨³ ´ µ �� ¶ � �¦ ¤ ¥ · ª³ ±   � ¡ ¶ � �¦ ¤ ¥ · ª³ ± ¸ £ £ ¬

¹ ¹ ¤ ¥ ¦ ¨³ ´ µ �� ¶ ° ª ¤ ¥ · ª³ ±   � ¡ ¶ ° ª ¤ ¥ · ª³ ± ¸ £ £

¬ ¹ ¹ �� ¶ ª ²± £ £ � ¡ ¶ ª ²±

Page 9: Structures and Enumerated Types - UNBaubanel/cs2023/structures.pdf · 2004-03-18 · Outcomes: Structures C for Java Programmers, Chapters 11 (11.1 to 11.4) and 12 (12.1 to 12.2),

Structures & Pointers

º »!¼ ½ ¾ » ¿À Á¼ Â

ÃÄ ½ ÅÆ Ç ÈÉ

ÃÄ ½ ÅÆ Ç ÊÉ

Ë ÌÎÍ Ï ¿ É

» Ê ¿ Ç Ã Ç Ð º »¼ ½ ¾ » ¿À Á¼ Â

ÃÄ ½ ÅÆ Ç ÈÉ

ÃÄ ½ ÅÆ Ç ÊÉ

Ë ÑÀ Á¼ Ò Í Ï ÑÀ Á¼ Ò Ñ É

ÑÀ ¼ » Ò ÈÉ

ÑÀ Á¼ Ò Ñ ¿ É

Page 10: Structures and Enumerated Types - UNBaubanel/cs2023/structures.pdf · 2004-03-18 · Outcomes: Structures C for Java Programmers, Chapters 11 (11.1 to 11.4) and 12 (12.1 to 12.2),

Structures & Pointers

Memory for pointers to structures must be initialized in same way as for other pointers:– using address of another structure

– using dynamic memory allocation

Ó:Ô ÕÖ ×Ö Ø Ù Ó!Ú Û Ü Ó ÕÝ ÞÚ ß

×à Û áâ Ö ãä

×à Û áâ Ö Ô ä

å æÝ ÞÚ çéè ê æÝ ÞÚ ç æëä

æÝ ÞÚ ç ìä

æÝ ÞÚ ç æ Õ í î ìä

Page 11: Structures and Enumerated Types - UNBaubanel/cs2023/structures.pdf · 2004-03-18 · Outcomes: Structures C for Java Programmers, Chapters 11 (11.1 to 11.4) and 12 (12.1 to 12.2),

Structures & Pointers

ïñð òó ôó õ ö ïø÷ ù ú ï òû ü÷ ý

ôÿþ ù �� ó ��

ôÿþ ù �� ó ð �

� �û ü÷ ��� �û ü÷ � ��

�û ü÷ � � ��

ü õ� � � �û � � þ ú � ö ü�� ó þ õ� �û ü÷ � � � � �� � � � � �

� þ ÷ �

ü õ� � � �û � � þ ú � ö ü�� ó þ õ� ö ï ÷ ù ú ï òû ü÷ � � � � � � � � �

�� � � � � � � ��� � � � �� � � �

�� �ð �� � �

Page 12: Structures and Enumerated Types - UNBaubanel/cs2023/structures.pdf · 2004-03-18 · Outcomes: Structures C for Java Programmers, Chapters 11 (11.1 to 11.4) and 12 (12.1 to 12.2),

Structures & Pointers

If is a pointer to a structure that has a member �, then

! � gives access to �. "

#$&% ')( *

+

+ , - "

+ , - #

Page 13: Structures and Enumerated Types - UNBaubanel/cs2023/structures.pdf · 2004-03-18 · Outcomes: Structures C for Java Programmers, Chapters 11 (11.1 to 11.4) and 12 (12.1 to 12.2),

Size of a Structure

You cannot assume that the size of a structure is the same as the sum of the sizes of all its members, because the compiler may use padding to satisfy memory alignment requirements.

. /10 2 3 / 3 45

3 6)7 0 7 8

4)9 / : 8

; . 8

How large is <?

= Say int = 4 bytes and char = 1 byte, sizeof(s) is not necessarily = 5, since some O/S require that data begin on some multiple number of bytes (typically 4). So 7 could be followed by a three-byte hole. Can also have holes at end of a structure.

Page 14: Structures and Enumerated Types - UNBaubanel/cs2023/structures.pdf · 2004-03-18 · Outcomes: Structures C for Java Programmers, Chapters 11 (11.1 to 11.4) and 12 (12.1 to 12.2),

Structures and Functions

>@? AB CB D E >1F G H > AI JF K

C)L G MN B OP

C)L G MN B ? P

Q RI JF S�T U RI JF S R P

RI JF S H L V E > F G H > L F W G V H X C L G MN B OT C L G MN B ? Y K

RI JF S A P

A[Z O \ O P

A[Z ? \ ? P

F B > GF V A P

QRI JF S ] \ H L V E >1F G H > L F W G V H X^ T _ Z _ Y P ` U ab Rc U `

Page 15: Structures and Enumerated Types - UNBaubanel/cs2023/structures.pdf · 2004-03-18 · Outcomes: Structures C for Java Programmers, Chapters 11 (11.1 to 11.4) and 12 (12.1 to 12.2),

Structures and Functions

d Previous function inefficient because structure is created on function's stack, then copied to calling function's stack

d Use call by reference instead

Page 16: Structures and Enumerated Types - UNBaubanel/cs2023/structures.pdf · 2004-03-18 · Outcomes: Structures C for Java Programmers, Chapters 11 (11.1 to 11.4) and 12 (12.1 to 12.2),

e f gh i fj k l1m n i l fm o p o&q gm r o l s g k[t

h f n uv w xt h f n uv w y z {

l s g k| } x ~ x�

l s g k| } y ~ y�

�o&q gm r ��

o&q gm r o � �

i f j k l1m n i l fm o p� �t �t � z � �� i f � y f j v y h f n uv w k � �

i f j k l1m n i l fm o p �t �t � z �

g � p p � ~ �q v v f i p k g�� w f � p o&q gm r z z z ~ ~ �� � � z �� � � � �

i f j k l1m n i l fm o p �t �t � z �

Page 17: Structures and Enumerated Types - UNBaubanel/cs2023/structures.pdf · 2004-03-18 · Outcomes: Structures C for Java Programmers, Chapters 11 (11.1 to 11.4) and 12 (12.1 to 12.2),

�&� �)� � � �� �� � � � � � � � �� � � �  ¡ ¢¤£ � � � �  ¡ ¥ ¦ §

¨© �  � ¡ � � � ¡ � ª � � � � �  ¡ «� � � ¡ �     � � � � � � � © ¨

�&� � � � � ª¬

� « � � ª ­ ®�     � � � � ��¯ ¡ � « � �� � � � ¦ ¦ ¦ ­ ­ °± ² ² ¦

� ¡ � �� � °± ² ²¬

ª³ ´ ¢ ­ ¢¬

ª³ ´ ¥ ­ ¥¬

� ¡ � �� � ª¬

µ� � � � � ® ª � � ¡ � �� � � � �� � � � � ª £ �� � � � �&� � � � � ¶ ¦ §

� ¡ � �� � ª ³ ´ ¢ ­ ­ ¶³ ´ ¢ · · ª³ ´ ¥ ­ ­ ¶³ ´ ¥¬

µ

Page 18: Structures and Enumerated Types - UNBaubanel/cs2023/structures.pdf · 2004-03-18 · Outcomes: Structures C for Java Programmers, Chapters 11 (11.1 to 11.4) and 12 (12.1 to 12.2),

¸º¹ »½¼ ¾ ¸ ¿ À Á Âà ÄÅ Æ ¼ Ç Â Æ Ã ¼ È ÀÉ Ê ËÍÌ

¸º¹ »½¼ ¾ ¸ ¿ Ê Á Âà ÄÅ Æ ¼ Ç Â Æ Ã ¼ È ÀÉ

Î ËÍÌ

» Ä Æ » Á ÂÃ Ï ¿ ¹ ¼ Ð È ¿ ÀÉ ¿ Ê ËÌ

Ѽ Ð Ð È ¿ À ËÌ

Ѽ Ð Ð È ¿ Ê ËÌ

Avoid leaving garbage:

Ò Ó ÔÕ Ö× ØÙ Ú Û × ÜÝ ÔÕ Þß à Ù á Ô à Õ Ù Ûâ ã ä Ý å æ æÍç

Page 19: Structures and Enumerated Types - UNBaubanel/cs2023/structures.pdf · 2004-03-18 · Outcomes: Structures C for Java Programmers, Chapters 11 (11.1 to 11.4) and 12 (12.1 to 12.2),

Blocks of Structures

èêé ëºì í è ì îï ð é ñ ò ó îô

èêé ëºì í è é õö ô

÷ºø õ ù ó î öûú üô

ë ýþ þ ì îï ð é ñ ò ó îÿ�é ó ó ø ï þ �� � ë�� î ø ý þ è é ëì í � � � ÿ ÿ �� � � � � �

��� � ��� �� � � �� �� � � � �� � �� � � � � �� � � � � !� � �� " #

$ � % � � � �& ' � � � � �)( � * � � + � � ,� � � �- . & " �

% � � - � � � � �& / � � / � �& 0 1� 0 132 " 4 � 5 " 67 � � � � � 7 6

+ � �� 8�

� � �- � � � � � � � 9 �� �� 0 � 0 2 " �

:

;< = >)? @A B<

pairpairpairpair

Page 20: Structures and Enumerated Types - UNBaubanel/cs2023/structures.pdf · 2004-03-18 · Outcomes: Structures C for Java Programmers, Chapters 11 (11.1 to 11.4) and 12 (12.1 to 12.2),

Blocks of Structures

C�D E C�FG�H I J C K L F C M NOF CQP P R

S I C D E G JT U V I E VW X Y K J X G X G RZ D T [C [ I V\ E] D ^ _ V ` Ca b W [ I V \ E] D ^ _ V ` Ca b c R F

Page 21: Structures and Enumerated Types - UNBaubanel/cs2023/structures.pdf · 2004-03-18 · Outcomes: Structures C for Java Programmers, Chapters 11 (11.1 to 11.4) and 12 (12.1 to 12.2),

Structures and Arrays

d egf hi j f kl m no

prq s f e f h t pvu w x p y

x zg{ u j{ |f } kl mQ~ � ���

��{ i u � � f u p i x f t }� � �

� �f x p { j� � f ��� � �f x p { j � � f � ��

�f x p { j � � f � u f x p { j � � f �

1,2 3,4 5,6 7,8R1

��� � � �� � ��� ��� � � � � �� � �

Page 22: Structures and Enumerated Types - UNBaubanel/cs2023/structures.pdf · 2004-03-18 · Outcomes: Structures C for Java Programmers, Chapters 11 (11.1 to 11.4) and 12 (12.1 to 12.2),

� � �� �   �¡ ¢�£ �¤ ¥ ¦ §©¨ £ ¦)ª ¤ « ¬ ¨ ­® ¥ ¯ °

�¤ ¦ �²±³´ �¤ ¦ µ ¢¶ § ¨ £ ¦ ª ¤ « ¬ ¨ · ¥ ¸¤ ¶ ¹ ¥3º » ¤ ª ¼¨ ¯ ±

µ � ´ ¢ � ½ ¾ ± � ¿ À ± �ÂÁ Á ¯

³ ´ �¤ ¦ µ ¢¶ �¨ ´ ¦ ¨ à ·� ½ ¢ · µ · µ ¯ ¸¤ ¶ ¹ � ¹¥3º » �¨ ´ ¦ �£ ¨ ¥ Ä �Å Æ Ã ¹ ¥3º » �¨ ´ ¦ �£ ¨ ¥ Ä � Å Æ Ç ¯ ±

È¥3º » ¤ ª ¼¨ the array of charactersÉ3Ê ËÌ ÍÎ Ï Ð�Ñ Í É the array of pairsÒ3Ó ÔÕ Ö× Ø Ù�Ú Ö Ò Û ÙÜ

the

Ý-th pairÞ3ß àá âã ä å�æ â Þ ç åè é ê the ë-coordinate of the

ì

-th pair

Page 23: Structures and Enumerated Types - UNBaubanel/cs2023/structures.pdf · 2004-03-18 · Outcomes: Structures C for Java Programmers, Chapters 11 (11.1 to 11.4) and 12 (12.1 to 12.2),

Initialization of Structures

írî ïð ñð ò ó ívô õ ö í ÷

ñgø õ ùú ð ûü

ñgø õ ùú ð î ü

ý þ�ÿ �ô � ü

þÿ �ô � � � ÷� � �� � ý ü

írî ïð ñð ò ó ívô õ ö í ÷

ö ÿ ô ÿ �ð � �� � � � ü

þ�ÿ �ô � �ð ô í � öð ó � � � ü

ý �ð ö í ÿ � ú ð � ü

�ð ö í ÿ � ú ð � ó � ÷ � ò �ô ó í � �÷ ÷� � � ý� ÷� � � ý� ÷ �� � ý�÷ �� � ý ý ý ü

Page 24: Structures and Enumerated Types - UNBaubanel/cs2023/structures.pdf · 2004-03-18 · Outcomes: Structures C for Java Programmers, Chapters 11 (11.1 to 11.4) and 12 (12.1 to 12.2),

Example: Quadratic Equation

��� �� �� � �"! # $ � % # & �! & � ' $ (

�*) # +, � &-

�*) # +, � + -

�*) # +, � $-

�*) # +, � ! ) ) � ./ 0 -

1 2 # & �3 -

'*4 � ! ) ) � 5 2 # & �3 6 % 7 -

8 66 9 ) , :� & % # & �! & � ' $ � % # & � '*) 4 �) ! ! � &, ! ) ) � <;

! � � #! 4 ' 4 = >@?6 A � � B� ! ) ) � &! � '*C & = ' 4 &! � ) ! & D D E ! � � #! 4 E?6 8

Page 25: Structures and Enumerated Types - UNBaubanel/cs2023/structures.pdf · 2004-03-18 · Outcomes: Structures C for Java Programmers, Chapters 11 (11.1 to 11.4) and 12 (12.1 to 12.2),

Example: Quadratic Equation

F*G H IJ J H"K LM N O PQ RTS U

VP*J N WX Y I Z S [ \ W R S [ \ W [ ]_^ ` R S [ \ O R S [ \a b

F c L S [ \ O Z Z ` d d I e ` ^ ` U

I Y H N I G ` b

I Z K S I H L I U b

S [ \ IJ J H f ` g Z L [S [ \ W h I Ui Lj ^ ` R S [ \ O U b

S [ \ IJ J H fk g Z L [S [ \ W [ I Ui Lj ^ ` R S [ \ O U b

I Y H N I G L k U b

l

Page 26: Structures and Enumerated Types - UNBaubanel/cs2023/structures.pdf · 2004-03-18 · Outcomes: Structures C for Java Programmers, Chapters 11 (11.1 to 11.4) and 12 (12.1 to 12.2),

Example: Quadratic Equation

mn o*p qr

stvu n wx yz p {

|} op ~ � q� � � y n � y yp ~ y} n � n p w � �p � r {

� � n p � q� � � � � � � � � �� � � yz p � n � � yz p � � � � yz p � �r {

o � q } � � ~ � q � yz p r � � �r

|} op ~ � q� � � � ~ � n } y � � � � �p � � yz p � } � � ~ �� � �

yz p � } � � ~ � � � r {

y � � y|} op ~ � q� � � � ~ � n } y o mn � op n } � �p � r {

Page 27: Structures and Enumerated Types - UNBaubanel/cs2023/structures.pdf · 2004-03-18 · Outcomes: Structures C for Java Programmers, Chapters 11 (11.1 to 11.4) and 12 (12.1 to 12.2),

Enumerated Types

Enumerated types are ordered collections of named constants; e.g.

�� � � �� � � � �� �

�¡  ¢ � � �¤£ ¥  ¢ � � �£ � �� ¦£ � � ��

§¡¨©«ª � � � � ¬ �� � � �� � � � � � �

�¡  ¢ � � �¤£ ¥  ¢ � � �£ � �� ¦£ � � ��

§ ­� � � � �� ®¨

�� � � �� � � � �� �¨

­� � � � � � ® ¬¨

Page 28: Structures and Enumerated Types - UNBaubanel/cs2023/structures.pdf · 2004-03-18 · Outcomes: Structures C for Java Programmers, Chapters 11 (11.1 to 11.4) and 12 (12.1 to 12.2),

Enumerated Types

¯ °²± ³ ± ´µ ¶ µ · µ ¸ ¶ ¸ ´

± ¶¹ º ¸» ¼ ¸ ³ ± ½ ¾

¿¡À Á ¿ ¹ ± Â ÃÀ Á ¿ ¹ ± Â » ¹ ½ ° Â » ¿ ¹ ½

Ä¡Å

introduces four constants:

Æ¡Ç È Æ¡É Ê, ËÌ Í Î¡Ï Ð, ÑÒ Ó Ô

, and

Õ Ö¡× Ø; all are of type convertible to

Ù²Ú Û

:

Ü¡Ý Þ Ü¡ß à represents the integer value 0

áâ ã ä¡å æ 1and so on

Page 29: Structures and Enumerated Types - UNBaubanel/cs2023/structures.pdf · 2004-03-18 · Outcomes: Structures C for Java Programmers, Chapters 11 (11.1 to 11.4) and 12 (12.1 to 12.2),

Enumerated Types

ç è²é ê ë²ì íì î ï²ð ñ ð ò ì ñ é ñó ôé íì î é è î«õ ö é ôì õ

ì ë²÷ ð øù ú ûü ýü þ û ÿ�� �� � � values:

�� � � � � � �

��� � � � � � �� �� � � � �� � �� � �

���

�� � � � � � ��

� � �� � � � ��

� �� � � � � ��

�� � � � � �� � � �� � � � �� �! � � � � "

Page 30: Structures and Enumerated Types - UNBaubanel/cs2023/structures.pdf · 2004-03-18 · Outcomes: Structures C for Java Programmers, Chapters 11 (11.1 to 11.4) and 12 (12.1 to 12.2),

Enumerated Types

#%$ &' ( &' ) ' * + ,.- */ + 0�$ * &' + - & * / $ 1' )2 ' 3 4 3

failure because a file can not be openedfailure because a file can not be closedsuccess

576 89 :9 ; 9 <= > ?

@ AB CD E @ FG AH C E @ AI

J @%K 8 9 L MON

Page 31: Structures and Enumerated Types - UNBaubanel/cs2023/structures.pdf · 2004-03-18 · Outcomes: Structures C for Java Programmers, Chapters 11 (11.1 to 11.4) and 12 (12.1 to 12.2),

Enumerated Types

Consider a function

P%Q RS T U R T Q VS W W XY Z

To output the result of calling this function as a string

[ \^] _ `a bc c ] d bc ef g h

ij kl b [ ] m mn o p b n q b m b r its

ij kl b [ ] m mn o p b [ l n c b r its

iu v [ [ bc c w vl n q b _ ] o k n m is

ix \ kc [ ] m mn o \] q q b m i

y{zq _ k m o w| i _ bc vl o n w [ ] l l k m d q _n [ b c c | } k c ~c � m i s

a bc c ] d bc e | k m o } q _ n [ bc c | }f z