chap4 - Chapter 4 Pointers and Dynamic Arrays 1 What Size...

Info iconThis preview shows pages 1–24. Sign up to view the full content.

View Full Document Right Arrow Icon
1 Chapter 4 Pointers and Dynamic Arrays
Background image of page 1

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
2 What Size Should We  Make the Checks Array? If it is too small, it might get filled up. If it is too large, many clients will only use a small fraction of the array space, and we will be wasting memory. The best approach might be to start an array off as small, then have it grow larger as more checks are written. This cannot be done with ordinary arrays, but it can be done with pointers and dynamically-allocated memory .
Background image of page 2
3 Memory Terminology variable name variable value address – a binary number used by the operating system to identify a memory cell of RAM It is important to know the precise meanings of these terms
Background image of page 3

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
4 Memory Terminology (cont.) Addresses 00110 01010 01110 10010 10110 x 15
Background image of page 4
5 Memory Terminology (cont.) Addresses 00110 01010 01110 10010 10110 x 15 Variable name
Background image of page 5

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
6 Memory Terminology (cont.) Addresses 00110 01010 01110 10010 10110 x 15 A variable (which is a location)
Background image of page 6
7 Memory Terminology (cont.) Addresses 00110 01010 01110 10010 10110 x 15 Value of the variable
Background image of page 7

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
8 Memory Terminology (cont.) Addresses 00110 01010 01110 10010 10110 x 15 Address of the variable
Background image of page 8
9 Pointers A pointer is a variable used to store an address The declaration of a pointer must have a data type for the address the pointer will hold (looks like this): int *ptr; char *chptr;
Background image of page 9

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
10 Location Behavior A variable is a location When locations are used in code, they behave differently depending on whether or not they are used on the left side of an assignment If not used on the left side of an assignment, the value at the location is used When used on the left side of assignment, the location itself is used
Background image of page 10
11 Address-of operator An address can be assigned to a pointer using the address-of int *ptr; int x; ptr = &x;
Background image of page 11

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
12 x ptr Addresses 00110 01010 01110 10010 10110 x ptr 01010 Result
Background image of page 12
13 Dereference Operator The dereference operator, *, is used on a pointer (or any expression that yields an address) The result of the dereference operation is a location (the location at the address that the pointer stores) Therefore, the way the dereference operator behaves depends on where it appears in code (like variables)
Background image of page 13

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
14 int x = 3, *ptr; ptr x 3
Background image of page 14
15 ptr = &x;  (what happens?) ptr x 3
Background image of page 15

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
16 ptr x 3 ptr = &x; 
Background image of page 16
17 y = *ptr + 5;  (what happens?) ptr x 3 y
Background image of page 17

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
18 ptr x 3 y 8 y = *ptr + 5;
Background image of page 18
19 int z = 5; ptr x 3 y 8 z 5
Background image of page 19

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
20 *ptr = 10 + z;  (what happens?) ptr x y 8 z 5 3
Background image of page 20
21 ptr x y 8 z 5 15 *ptr = 10 + z;
Background image of page 21

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
22 *ptr = 10 + z; ptr x y 8 z 5 15 Note that the value of x changes even though this line of code doesn’t contain x
Background image of page 22
23 Arrays The address of an array is the same as the address of the first element of the array. An array’s name (without using an index) contains
Background image of page 23

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Image of page 24
This is the end of the preview. Sign up to access the rest of the document.

This note was uploaded on 04/29/2008 for the course CS 310 taught by Professor Dr.panja during the Spring '08 term at Morehead State.

Page1 / 76

chap4 - Chapter 4 Pointers and Dynamic Arrays 1 What Size...

This preview shows document pages 1 - 24. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online