35
Pointers & Arrays CS2023 Winter 2004

Pointers & Arrays - UNBaubanel/cs2023/pointers_arrays.pdf · Outcomes: Pointers & Arrays fiC for Java Programmersfl, Chapter 8, section 8.12, and Chapter 10, section 10.2 Other

  • Upload
    others

  • View
    13

  • Download
    0

Embed Size (px)

Citation preview

Page 1: Pointers & Arrays - UNBaubanel/cs2023/pointers_arrays.pdf · Outcomes: Pointers & Arrays fiC for Java Programmersfl, Chapter 8, section 8.12, and Chapter 10, section 10.2 Other

Pointers & Arrays

CS2023 Winter 2004

Page 2: Pointers & Arrays - UNBaubanel/cs2023/pointers_arrays.pdf · Outcomes: Pointers & Arrays fiC for Java Programmersfl, Chapter 8, section 8.12, and Chapter 10, section 10.2 Other

Outcomes: Pointers & Arrays

� “C for Java Programmers”, Chapter 8, section 8.12, and Chapter 10, section 10.2

� Other textbooks on C on reserve

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

� Identify how pointers and array are similar and how they are different

� Use pointers to traverse C arrays (1D and 2D) and pointer subtraction to count how many elements have been traversed

� Use pointers to pass portions of C arrays to functions

Page 3: Pointers & Arrays - UNBaubanel/cs2023/pointers_arrays.pdf · Outcomes: Pointers & Arrays fiC for Java Programmersfl, Chapter 8, section 8.12, and Chapter 10, section 10.2 Other

Arrays and Pointers

� A single-dimensional array is a typed constant pointer initialized to point to a block of memory that can hold a number of objects.

��� � �� � � �

��� � ��� �� �

��

is an

� � �

pointer that points to a block of memory that can hold 1000 integer objects

� ��

is a pointer to

� � �.

�� � � �� �

� �� � �� �

Page 4: Pointers & Arrays - UNBaubanel/cs2023/pointers_arrays.pdf · Outcomes: Pointers & Arrays fiC for Java Programmersfl, Chapter 8, section 8.12, and Chapter 10, section 10.2 Other

Arrays and Pointers

� When an array is declared, compiler allocates enough contiguous space in memory to contain all the elements of array��� � � �� � � �� ��� �

� Base address of array is address of first element of array. Say bytes numbered 300, 304, 308, ..., 696 are alocated as addresses of a[0], a[1],... a[99]

� � � � is equivalent to � � � � � � �

Causes 300 to be assigned to p

0 1 2 3 4 5 ... 99

!

"

Page 5: Pointers & Arrays - UNBaubanel/cs2023/pointers_arrays.pdf · Outcomes: Pointers & Arrays fiC for Java Programmersfl, Chapter 8, section 8.12, and Chapter 10, section 10.2 Other

Arrays Assignment and Pointers

# Can access first element of an array using:

$ %& ' ( )+*

# or through pointer:

, ( $*

- , ( )*

# Can access other elements of a using pointer arithmetic

. 0 1 2 3 4 5 ... 99

/0

Page 6: Pointers & Arrays - UNBaubanel/cs2023/pointers_arrays.pdf · Outcomes: Pointers & Arrays fiC for Java Programmersfl, Chapter 8, section 8.12, and Chapter 10, section 10.2 Other

Pointer Arithmetic

Valid operations on pointers include:

- the sum of a pointer and an integer- the difference of a pointer and an integer- pointer comparison- the difference of two pointers.

Page 7: Pointers & Arrays - UNBaubanel/cs2023/pointers_arrays.pdf · Outcomes: Pointers & Arrays fiC for Java Programmersfl, Chapter 8, section 8.12, and Chapter 10, section 10.2 Other

Sum of Pointer and Integer

1 To access other objects in the block of memory pointed to 2, use

2 3 4

where 4 is an integer. This expression yields a pointer to the n-th object beyond the one that 2 currently points to.

1 The exact meaning of "object" is determined by the type of 2.

Page 8: Pointers & Arrays - UNBaubanel/cs2023/pointers_arrays.pdf · Outcomes: Pointers & Arrays fiC for Java Programmersfl, Chapter 8, section 8.12, and Chapter 10, section 10.2 Other

Sum of Pointer and Integer

5�6 7 8 9: ;< =�> ?

> @ 8 ?

= > @ A ?

= B > C A D @ E ?

= B > C E D @ : ?F G H

I

JI K L I K M

Page 9: Pointers & Arrays - UNBaubanel/cs2023/pointers_arrays.pdf · Outcomes: Pointers & Arrays fiC for Java Programmersfl, Chapter 8, section 8.12, and Chapter 10, section 10.2 Other

Sum of Pointer and Integer

N�O P Q RS T UV W�X V W�Y Z

X [ Q \ ] Z

W�X [ ^ Z

Y [ X \ ^ Z

W Y [ _ Z

X \ [ _ Z

W�X [ ` Z

a 0 1 2 3 4 5 6 7 8 9

bc

a d

0 1 2 3 4 5 6 7 8 9

bc

ea d

0 1 2 3 4 5 6 7 8 9

c f

e b

Page 10: Pointers & Arrays - UNBaubanel/cs2023/pointers_arrays.pdf · Outcomes: Pointers & Arrays fiC for Java Programmersfl, Chapter 8, section 8.12, and Chapter 10, section 10.2 Other

Sum of Pointer and Integer: ith object

g�h ij j kl l m n�o prq m n s t o u m v�w i x k xh yz

{| h j } ~

� ��� � v�

or

� � v�

� �� �

and

�� are equivalent.

Page 11: Pointers & Arrays - UNBaubanel/cs2023/pointers_arrays.pdf · Outcomes: Pointers & Arrays fiC for Java Programmersfl, Chapter 8, section 8.12, and Chapter 10, section 10.2 Other

Sum of Pointer and Integer: ith object

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

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

� � � �

�� � � � �   � � ¡ �� � � � � ¢ ¢ £

� � ��¤ ¥ � � � �¦ §� �¦ � � ¢ � £ � �   £ � �� ¨ �

� © � ª � � «� ¬­

®¯ � °± ²� £ �

� � � ¢ �� � � £ � ³´ µ �

Page 12: Pointers & Arrays - UNBaubanel/cs2023/pointers_arrays.pdf · Outcomes: Pointers & Arrays fiC for Java Programmersfl, Chapter 8, section 8.12, and Chapter 10, section 10.2 Other

Difference of Pointer and Integer

Often, we need to access objects preceding the object pointed to by a pointer ¶·

¶ ¸ ¹

yields a pointer to the n-th object before the one that º currently points to.

Page 13: Pointers & Arrays - UNBaubanel/cs2023/pointers_arrays.pdf · Outcomes: Pointers & Arrays fiC for Java Programmersfl, Chapter 8, section 8.12, and Chapter 10, section 10.2 Other

Difference of Pointer and Integer

»�¼ ½ ¾ ¿À Á Âà Ä�Å Ã Ä�Æ Ç

Å È ¾ É Ê Ç

Ä�Å È Ë Ç

Æ È Å Ì Í Ç

Ä Æ È Î Ç

Å Ì È Î Ç

Ä�Å È Í Ç

0 1 2 3 4 5 6 7 8 9

Ï

Ð Ñ

Ò

0 1 2 3 4 5 6 7 8 9

Ó

Ô Õ

Ö× Ø

0 1 2 3 4 5 6 7 8 9

Ù Ú

ÛÜ

Page 14: Pointers & Arrays - UNBaubanel/cs2023/pointers_arrays.pdf · Outcomes: Pointers & Arrays fiC for Java Programmersfl, Chapter 8, section 8.12, and Chapter 10, section 10.2 Other

Example

Ý Þàß áâ ãß äå æç è

â ã é êìë í êìî í ï ð äå æç ñóò

ë ô ïò

õ ö ê â ã â é â ï ÷ â ø ß é ùàß ú ÷àû ü ý ê ö

ö ê û þ éë þ é â ã ÿ ß � ß ÿ� ß û ÿ Þàß ÿ ê ö

áû ÿ � â ô � í î ô ë � ä å æç � �ò â � ä å æç ò â � � �

ë ÿ â ã é á � á� ã í ê � î � â � �ò

� � è

��� � ��� ��

Page 15: Pointers & Arrays - UNBaubanel/cs2023/pointers_arrays.pdf · Outcomes: Pointers & Arrays fiC for Java Programmersfl, Chapter 8, section 8.12, and Chapter 10, section 10.2 Other

Careful!

Given a block of memory of

�� ��

objects, pointed to by �, you can set � to point to the last object in the block using:

� � �� �� !

rather than

" # $% &'

(‘off by one’ error).

Page 16: Pointers & Arrays - UNBaubanel/cs2023/pointers_arrays.pdf · Outcomes: Pointers & Arrays fiC for Java Programmersfl, Chapter 8, section 8.12, and Chapter 10, section 10.2 Other

Pointer Comparison

( Two pointers of the same type, ) and *, may be compared as long as both of them point to objects within a single memory block

+ Pointers may be compared using the <, >, <=, >=, == , !=

, When you are comparing two pointers, you are comparing the values of those pointers rather than the contents of memory locations pointed to by these pointers

Page 17: Pointers & Arrays - UNBaubanel/cs2023/pointers_arrays.pdf · Outcomes: Pointers & Arrays fiC for Java Programmersfl, Chapter 8, section 8.12, and Chapter 10, section 10.2 Other

Pointers and traversalsAssuming-/. 0 12 3 465 7 4 5 8:9

and ; pointing to an array of

<= >?

doubles:

@:A B C/D E F D G D B A H:I J K F LNM D E O D P QR ST M D E P P U

D B A H I J K V F VD EM

V D B E/W K X/Y J Z:[ Y B H/\ V

@:A B C/D E F D P QR ST ] LM D E ^ F D M D E ] ] U

D B E/W K @ C_ ` @ W _ G VD E U M

Page 18: Pointers & Arrays - UNBaubanel/cs2023/pointers_arrays.pdf · Outcomes: Pointers & Arrays fiC for Java Programmersfl, Chapter 8, section 8.12, and Chapter 10, section 10.2 Other

Block Traversal Idiom

ab c d e f d g h ij kl f d h h m

n o p pq p

Page 19: Pointers & Arrays - UNBaubanel/cs2023/pointers_arrays.pdf · Outcomes: Pointers & Arrays fiC for Java Programmersfl, Chapter 8, section 8.12, and Chapter 10, section 10.2 Other

Example: largest element

rs tvu wyx z{ w | u }�~ z� u � w u } u �u { w �{ ~ z z~ � � s r

��� x � } u � �{ �� }�~ z� u � w � �� x � } u � �� � ��{ w { �

�� � x � } u s �~ � � s � ���

�� z � �~ � � � � � � � � � � � � � � � � { � � � � � �

� � � s �~ � � s � � �

�~ � � � ���

z u w x z{ s �~ ��

Page 20: Pointers & Arrays - UNBaubanel/cs2023/pointers_arrays.pdf · Outcomes: Pointers & Arrays fiC for Java Programmersfl, Chapter 8, section 8.12, and Chapter 10, section 10.2 Other

Example: block copy

Copy the contents of a block (or array) pointed to by � of size

�� ��

to another block pointed to by �:

�/  ¡ ¢£ ¤ ¥6¦ §¨ ¥6© §:ª

�/  ¡ ¢£ ¤ ¦ «¬ ­® ¯ ° ¨ © «¬ ­ ® ¯ °

±  ² ³ © § ´ © ¨ ¦ § ´ ¦ ª © § µ © ¶¬ ­ ® ¯ª © § ¶ ¶ ¨¦ § ¶ ¶ ·¥6© § ´ ¥ ¦ §ª

Page 21: Pointers & Arrays - UNBaubanel/cs2023/pointers_arrays.pdf · Outcomes: Pointers & Arrays fiC for Java Programmersfl, Chapter 8, section 8.12, and Chapter 10, section 10.2 Other

Pointer Subtraction

Given two pointers, ¸ and ¹, such that:• both pointers are of the same type, • º > »

• both point to objects in a single memory block, the expression

¼ ½ ¾

yields the number of objects between ¿ and À

,

including the object pointed to by Á. The type of the result of pointer difference is  ÃyÄ ÅÆ Ç ÇÈ Ã

, defined in É Ê Ë Ë/Ì ÍÎ Ï

.

Page 22: Pointers & Arrays - UNBaubanel/cs2023/pointers_arrays.pdf · Outcomes: Pointers & Arrays fiC for Java Programmersfl, Chapter 8, section 8.12, and Chapter 10, section 10.2 Other

Pointer Subtraction

Ð/Ñ Ò Ó ÔÕ Ö ×Ø Ù6Ú Ø Ù6Û Ü

Ú Ý Ó Ü

Û Ý Ó Þ ß Ü

Ú à ÐÑ Ò áâ ãä å ãØ Û æ Ú ç ÜOutput: 3

0 1 2 3 4 5 6 7 8 9

èé

ê

Page 23: Pointers & Arrays - UNBaubanel/cs2023/pointers_arrays.pdf · Outcomes: Pointers & Arrays fiC for Java Programmersfl, Chapter 8, section 8.12, and Chapter 10, section 10.2 Other

Example

Find the first occurrence of the value

ë

in a block of integers (

ì íî

if not found):ïñð ò óô õ ï ò ïô ðö

ïñð ò ÷ óùø ÷ ó ïö

ú ÷ ó ïñð ï ò ïñû ü ïþý ÿ � ÷ ú

� � ��ô � � ó ï � óö ó ï � ó � � � ö ó ï � � �

ï � � ÷ ó ï � � �

� � ÿ û �ö

óô õ ï ò ïô ð � � ó ï � � ó � � � � � � �� � ó ï�� ó � �ö

Page 24: Pointers & Arrays - UNBaubanel/cs2023/pointers_arrays.pdf · Outcomes: Pointers & Arrays fiC for Java Programmersfl, Chapter 8, section 8.12, and Chapter 10, section 10.2 Other

Example

Find the first occurrence of the value

in a block of integers (

� ��

if not found):��� � � ! � � � � "

��� � # �%$ # � � "

& # � ��� � � ��' ( �*) + , # &

- - -. / 0 � � 1 � " � � 2 � 3 45 67 " � � 3 3 8

� . 0 # � � 1 1 9 8

: / +' ; "

� ! � � � � 1 0 � � 1 1 � 3 45 67 8 < 7 => ? � �A@ � 3 B "C D E

FG

F HIJK LNM L JO = 3

Page 25: Pointers & Arrays - UNBaubanel/cs2023/pointers_arrays.pdf · Outcomes: Pointers & Arrays fiC for Java Programmersfl, Chapter 8, section 8.12, and Chapter 10, section 10.2 Other

Arrays as Parameters

When arrays are used as function parameters, they are actually

treated as pointers. The following two declarations are equivalent:

PRQ S TU V PW XY Z[ \] ^ U _ _ `a b PRQ S c Ped ^ feg

PRQ S T U V Ph X Y Z[ \] ^ i U _ _ b PRQ S c Pd ^ fg

The second parameter is necessary to specify the size of the array.

Page 26: Pointers & Arrays - UNBaubanel/cs2023/pointers_arrays.pdf · Outcomes: Pointers & Arrays fiC for Java Programmersfl, Chapter 8, section 8.12, and Chapter 10, section 10.2 Other

jlk m no p qr n n ps t jlk m u vw x j k m y j{z o | }

j k m ~�� j{�

�{� n t � j � u� � j � � j � y j{z o � � j � � |

j � t y� pk � t� � q� x � j | � � � |

no m�� nk � �

n o m�� nk � �

�� � j q � n jlk m r n n ps t j k m u vw x jlk m y j z o | }

j k m ~�� j{�

�{� n t � j � u� � j � � j � y j{z o � � j � � |

� n jlk m � t � q� x ~�� j | ��

Page 27: Pointers & Arrays - UNBaubanel/cs2023/pointers_arrays.pdf · Outcomes: Pointers & Arrays fiC for Java Programmersfl, Chapter 8, section 8.12, and Chapter 10, section 10.2 Other

�� ��� � �� �� � � �� � � � �� �   � � � � ¡ � �   � �� � � � � � � ¡ � �

¢  � �� � � £¤ ¥¦ §¨

  �© ª �l�  « £¤ ¥¦ ¬{­

� �® � � �   � � � � ¡ ®  °¯ £ ¤ ¥¦ ± ² ² ¨ ±

³´ ´ µ´ ¶

·¸ ¹lº » ¼¸ ¸ ½¾ ¿À Á Âà ÄÅ Æ ¶

·¸ ¹lº » ¼¸ ¸ ½¾ ¿À Á Âà ÄÅ Ç È É Æ ¶ ÊË ·¸ Ì Í ¹{Î Ë Ê

·¸ ¹lº » ¼¸ ¸ ½¾ ¿À Ï Ð Á Âà ÄÅ Ç Ð Æ ¶ Ê Ë ÑÒ Í Í ¹Î Ë Ê

·¸ ¹lº » ¼¸ ¸ ½¾ ¿À Ï Ð Á Ó Æ ¶ Ê Ë Ñ ÌÔ Õ Ì º » Ë Ê

Page 28: Pointers & Arrays - UNBaubanel/cs2023/pointers_arrays.pdf · Outcomes: Pointers & Arrays fiC for Java Programmersfl, Chapter 8, section 8.12, and Chapter 10, section 10.2 Other

Are Arrays and Pointers Interchangeable?

Ö Array parameters are interchangeable with pointer parameters, but array variables are not the same as pointer variables.

× C compiler converts name of an array to a constant pointer when necessary

Ø sizeof(array) returns number of bytes occupied by array, whereas sizeof(pointer) returns number of bytes used to store pointer

Page 29: Pointers & Arrays - UNBaubanel/cs2023/pointers_arrays.pdf · Outcomes: Pointers & Arrays fiC for Java Programmersfl, Chapter 8, section 8.12, and Chapter 10, section 10.2 Other

Are Arrays and Pointers Interchangeable?

ÙRÚ Û ÙÜ ÝÞ ß ß ß àeá

ÙRÚ Û âäã å Ù Ú Ûçæ è é Ü á

ê ëeì íî ïð ëñ ò

is

ó ô ô ôõ ö ÷eø ùú ûü ÷Rý þ ÿ

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

is the number of bytes used to

store a pointer to an

�� �.

Page 30: Pointers & Arrays - UNBaubanel/cs2023/pointers_arrays.pdf · Outcomes: Pointers & Arrays fiC for Java Programmersfl, Chapter 8, section 8.12, and Chapter 10, section 10.2 Other

Pointers & Multi-Dimensional Arrays

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

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

) Arrays stored in row-major order

0 1 2543

*+ , -*+ , .

/

0 12 30 14 3

0 1 2 543

56 7 8 56 7 9

Page 31: Pointers & Arrays - UNBaubanel/cs2023/pointers_arrays.pdf · Outcomes: Pointers & Arrays fiC for Java Programmersfl, Chapter 8, section 8.12, and Chapter 10, section 10.2 Other

Pointers & Multi-Dimensional Arrays

:<; = > ?@ AB C D EF G H ?@ AB C I EJ G HLK:<; = MN OQP R N SK

TN M U MN O V WK MN O X @ AB C D EF GK M N O Y Y Z

TN M U RN S V WK RN S X @ AB C I EJ GK RN S Y Y Z

> ? MN O H ? RN S H V WK

View [ as a one-dimensional array of integers:

\<] ^ _a` b

cLd e f ` g hji kl m kl m b ` n g hi ko pq r s tu vxw y m ko pq r z t{ v w y m b

` | | }

_a` g l b

Page 32: Pointers & Arrays - UNBaubanel/cs2023/pointers_arrays.pdf · Outcomes: Pointers & Arrays fiC for Java Programmersfl, Chapter 8, section 8.12, and Chapter 10, section 10.2 Other

Pointers & Multi-Dimensional Arrays

Point to row

~

in array �:

�<� � �a� �

� � �j� � �� �� � �

or:

�� � �� �L�

since � �� �

is the

th row of �

contrast with single-dimensional array where � �� �

equivalent to

� ��   ¡¢

Page 33: Pointers & Arrays - UNBaubanel/cs2023/pointers_arrays.pdf · Outcomes: Pointers & Arrays fiC for Java Programmersfl, Chapter 8, section 8.12, and Chapter 10, section 10.2 Other

Pointers & Multi-Dimensional Arrays

Clear row

£

of array ¤:

¥<¦ § ¥<¦ § ¨ ©ª «¬ ­ ® ¯° ± ² ©ª «¬ ­ ³ ¯´ ± ²¶µ ·a¸ µ ¥L¹

ºL» ¼ ½ ¸ ¾ ¨ © ¥ ² ¹ ¸ ¿ ¨ © ¥ ² À ª «¬ ­ ³ ¯´ ± ¹ ¸ À À Á

·a¸ ¾ Â ¹

Can pass à ÄÅ Æ

to a function that is expecting a one-dimensional array as its argument. Find largest element in row

Ç

of È:

É<Ê ËÌ ÍÎ Ï Ð ÑÒ Ó ÔÕ É<Ê ËÌ ÍÎ Ï Ö Ê × Ò Ø¶Ù ÚÛ ÜÕ ÝÞ ß à áLâ

Page 34: Pointers & Arrays - UNBaubanel/cs2023/pointers_arrays.pdf · Outcomes: Pointers & Arrays fiC for Java Programmersfl, Chapter 8, section 8.12, and Chapter 10, section 10.2 Other

Pointers & Multi-Dimensional Arrays

Caution:

• The base address of a two-dimensional array ã is äæå çè é çè é

, not ê. • The array name ë by itself is equivalent to

ìæí îï ð

Page 35: Pointers & Arrays - UNBaubanel/cs2023/pointers_arrays.pdf · Outcomes: Pointers & Arrays fiC for Java Programmersfl, Chapter 8, section 8.12, and Chapter 10, section 10.2 Other

Storage Mapping Function

ñò ó ô õö ÷ õø ÷�ù

ô õ ñ ÷ õú ÷

is equivalent to

û üý þ ÿ� � ÿ � � � � � � ��

• Note that column size is required in storage mapping function, which is why it must be specified in function declaration� � � � � � � � �� �� �� � �� � � �� � � �