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

Info icon This 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
Image of page 1

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

View Full Document Right 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 .
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
Image of page 3

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

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

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

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

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

View Full Document Right Arrow Icon
8 Memory Terminology (cont.) Addresses 00110 01010 01110 10010 10110 x 15 Address of the variable
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;
Image of page 9

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

View Full Document Right 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
Image of page 10
11 Address-of operator An address can be assigned to a pointer using the address-of operator &: int *ptr; int x; ptr = &x;
Image of page 11

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

View Full Document Right Arrow Icon
12 x ptr Addresses 00110 01010 01110 10010 10110 x ptr 01010 Result
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)
Image of page 13

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

View Full Document Right Arrow Icon
14 int x = 3, *ptr; ptr x 3
Image of page 14
15 ptr = &x;  (what happens?) ptr x 3
Image of page 15

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

View Full Document Right Arrow Icon
16 ptr x 3 ptr = &x; 
Image of page 16
17 y = *ptr + 5;  (what happens?) ptr x 3 y
Image of page 17

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

View Full Document Right Arrow Icon
18 ptr x 3 y 8 y = *ptr + 5;
Image of page 18
19 int z = 5; ptr x 3 y 8 z 5
Image of page 19

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

View Full Document Right Arrow Icon
20 *ptr = 10 + z;  (what happens?) ptr x y 8 z 5 3
Image of page 20
This is the end of the preview. Sign up to access the rest of the document.

{[ snackBarMessage ]}

What students are saying

  • Left Quote Icon

    As a current student on this bumpy collegiate pathway, I stumbled upon Course Hero, where I can find study resources for nearly all my courses, get online help from tutors 24/7, and even share my old projects, papers, and lecture notes with other students.

    Student Picture

    Kiran Temple University Fox School of Business ‘17, Course Hero Intern

  • Left Quote Icon

    I cannot even describe how much Course Hero helped me this summer. It’s truly become something I can always rely on and help me. In the end, I was not only able to survive summer classes, but I was able to thrive thanks to Course Hero.

    Student Picture

    Dana University of Pennsylvania ‘17, Course Hero Intern

  • Left Quote Icon

    The ability to access any university’s resources through Course Hero proved invaluable in my case. I was behind on Tulane coursework and actually used UCLA’s materials to help me move forward and get everything together on time.

    Student Picture

    Jill Tulane University ‘16, Course Hero Intern