DynamicAllocation - Review of Pointers and Dynamic Arrays...

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

View Full Document Right Arrow Icon
Source: Absolute C++, Savitch, Addison-Wesley Review of Pointers and Dynamic Arrays Dr. David A. Gaitros Department of Computer Science
Background image of page 1

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

View Full DocumentRight Arrow Icon
Source: Absolute C++, Savitch, Addison-Wesley Learning Objectives Pointers Pointer variables Memory management Dynamic Arrays Creating and using Pointer arithmetic Classes, Pointers, Dynamic Arrays The this pointer Destructors, copy constructors 10-2
Background image of page 2
Source: Absolute C++, Savitch, Addison-Wesley Pointer Introduction Pointer definition: Memory address of a variable Recall: memory divided Numbered memory locations Addresses used as name for variable You’ve used pointers already! Call-by-reference parameters Address of actual argument was passed 10-3
Background image of page 3

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

View Full DocumentRight Arrow Icon
Source: Absolute C++, Savitch, Addison-Wesley Pointer Variables Pointers are "typed" Can store pointer in variable Not int, double, etc. Instead: A POINTER to int, double, etc.! Example: double *p; p is declared a "pointer to double" variable Can hold pointers to variables of type double Not other types! 10-4
Background image of page 4
Source: Absolute C++, Savitch, Addison-Wesley Declaring Pointer Variables Pointers declared like other types Add "*" before variable name Produces "pointer to" that type "*" must be before each variable int *p1, *p2, v1, v2; p1, p2 hold pointers to int variables v1, v2 are ordinary int variables 10-5
Background image of page 5

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

View Full DocumentRight Arrow Icon
Source: Absolute C++, Savitch, Addison-Wesley Addresses and Numbers Pointer is an address Address is an integer Pointer is NOT an integer! C++ forces pointers be used as addresses Cannot be used as numbers Even though it "is a" number 10-6
Background image of page 6
Source: Absolute C++, Savitch, Addison-Wesley Pointing Terminology, view Talk of "pointing", not "addresses" Pointer variable "points to" ordinary variable Leave "address" talk out Makes visualization clearer "See" memory references Arrows 10-7
Background image of page 7

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

View Full DocumentRight Arrow Icon
Source: Absolute C++, Savitch, Addison-Wesley Pointing to … int *p1, *p2, v1, v2; p1 = &v1; Sets pointer variable p1 to "point to" int variable v1 Operator, & Determines "address of" variable Read like: "p1 equals address of v1" Or "p1 points to v1" 10-8
Background image of page 8
Source: Absolute C++, Savitch, Addison-Wesley Pointing to … Recall: int *p1, *p2, v1, v2; p1 = &v1; Two ways to refer to v1 now: Variable v1 itself: cout << v1; Via pointer p1: cout *p1; Dereference operator, * Pointer variable "derereferenced" Means: "Get data that p1 points to" 10-9
Background image of page 9

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

View Full DocumentRight Arrow Icon
Source: Absolute C++, Savitch, Addison-Wesley "Pointing to" Example Consider: v1 = 0; p1 = &v1; *p1 = 42; cout << v1 << endl; cout << *p1 << endl; Produces output: 42 42 p1 and v1 refer to same variable 10-10
Background image of page 10
Image of page 11
This is the end of the preview. Sign up to access the rest of the document.

This note was uploaded on 04/02/2012 for the course COP 3014 taught by Professor Gaitros during the Fall '11 term at Florida State College.

Page1 / 35

DynamicAllocation - Review of Pointers and Dynamic Arrays...

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

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