chapter 11 – pointer variables. declaring a pointer variable u declared with data type, * and...

27
Chapter 11 – Pointer Variables

Upload: allan-dickerson

Post on 17-Jan-2016

236 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Chapter 11 – Pointer Variables. Declaring a Pointer Variable u Declared with data type, * and identifier type* pointer_variable; u * follows data type

Chapter 11 – Pointer Variables

Page 2: Chapter 11 – Pointer Variables. Declaring a Pointer Variable u Declared with data type, * and identifier type* pointer_variable; u * follows data type

Declaring a Pointer Variable

Declared with data type, * and identifier type* pointer_variable;

* follows data type– Usually no space between so more obvious– Space allowed, so 1 or more would still work

Reserves space for storage Must initialize or assign address

Lesson 11.1

Page 3: Chapter 11 – Pointer Variables. Declaring a Pointer Variable u Declared with data type, * and identifier type* pointer_variable; u * follows data type

Assigning an Address

Use the "address of" operator (&) General form:

pointer_variable = &ordinary_variable

Lesson 11.1

Name of the pointer Name of ordinary variable

Page 4: Chapter 11 – Pointer Variables. Declaring a Pointer Variable u Declared with data type, * and identifier type* pointer_variable; u * follows data type

Using a Pointer Variable

Can be used to access a value Unary operator * used

* pointer_variable– In executable statement, indicates value

Common practice to use suffix ptr for pointer variables– Example: width_ptr

Lesson 11.1

Page 5: Chapter 11 – Pointer Variables. Declaring a Pointer Variable u Declared with data type, * and identifier type* pointer_variable; u * follows data type

Pointer Operators

Ampersand &– Address of

Asterisk *– Get value at the address

Lesson 11.1

Page 6: Chapter 11 – Pointer Variables. Declaring a Pointer Variable u Declared with data type, * and identifier type* pointer_variable; u * follows data type

Uses of *

Binary multiplication operator volume = height * depth * width;

Declaration specifier indicating address to be stored in variable's memory cell double* height_address;

Unary operator indicating to get value or access memory cells at addressed *height_address = 10.5;

Lesson 11.1

Page 7: Chapter 11 – Pointer Variables. Declaring a Pointer Variable u Declared with data type, * and identifier type* pointer_variable; u * follows data type

Uses of &

Declaration of function header to indicate a reference void function1 (double&)

In executable statement to indicate "address of" height_address = &height;

Lesson 11.1

Page 8: Chapter 11 – Pointer Variables. Declaring a Pointer Variable u Declared with data type, * and identifier type* pointer_variable; u * follows data type

Spacing a *

Somewhat flexible in declaration or header double* height; double *height; double * height;

Lesson 11.1

Confusing – looks likemultiplication usage

For multiple pointer declarations double *height *width;

double* height;double* width;

or

Page 9: Chapter 11 – Pointer Variables. Declaring a Pointer Variable u Declared with data type, * and identifier type* pointer_variable; u * follows data type

Transferring Addresses to Functions

In declaration and header use type* in argument list type function (type*, type*); type funcName (type* name, type* name)

In function call use &variable in argument list to pass address identifier = funcName (&name1, &name2);

Lesson 11.2

Page 10: Chapter 11 – Pointer Variables. Declaring a Pointer Variable u Declared with data type, * and identifier type* pointer_variable; u * follows data type

Using Pointer for Array Transfer

Lesson 11.3

rtype function (type*, int);

function (array, num);

rtype function (type* b, int num_elem) { code using b with brackets (b[n]) }

Declaration

Call

Header

FunctionBody

Page 11: Chapter 11 – Pointer Variables. Declaring a Pointer Variable u Declared with data type, * and identifier type* pointer_variable; u * follows data type

Example:

Lesson 11.3

void function2 (double*, int);int main ( ){ double c[5] = {2.1, 3.5, 6.4, 1.9, 4.5};

function2 ( c, 5);} void function2 (double* b, int num_elem)

Name of array is address

Page 12: Chapter 11 – Pointer Variables. Declaring a Pointer Variable u Declared with data type, * and identifier type* pointer_variable; u * follows data type

Pointer Variables and Math

Declare variable as pointer double* b;– Holds address

Can perform addition and subtraction– Purpose of add is to advance to subsequent cell

b + 1 (double is 8 bytes, so adds 8 bytes)– Subtraction goes back to previous memory cell

b – 1 (goes back 8 bytes)

Lesson 11.4

Page 13: Chapter 11 – Pointer Variables. Declaring a Pointer Variable u Declared with data type, * and identifier type* pointer_variable; u * follows data type

Returning Array Address from Function

Lesson 11.5

Example: Function declaration and header double* get_array_address (double [ ] ); double* get_array_address (double c[ ] )

Now a return statement with variable thatindicates address in the function body return c;

double*

Could also use

Page 14: Chapter 11 – Pointer Variables. Declaring a Pointer Variable u Declared with data type, * and identifier type* pointer_variable; u * follows data type

Pointer Variables

Point to 2, 4, or 8 bytes of memory depending on type of variable

Can point to entire array– Holds address of beginning of array– Pointer declaration indicates size of memory

greater than single value

Lesson 11.6

Page 15: Chapter 11 – Pointer Variables. Declaring a Pointer Variable u Declared with data type, * and identifier type* pointer_variable; u * follows data type

Creating Pointer to Arrays

Example: double (*f) [2];

Lesson 11.6

Declares f to be a pointerOne dimensional array of size 2

Array elements of type double

Example: double (*h) [3] [5];

Declares h to be a pointer

Two dimensional array of size 3 * 5 (15 elements)

Array elements of type double( ) are REQUIRED

Page 16: Chapter 11 – Pointer Variables. Declaring a Pointer Variable u Declared with data type, * and identifier type* pointer_variable; u * follows data type

typedef Statement

Used to create an alias for a data type– Note – not a new data type

Basic form typedef type synonym_1, synonym_n;

Lesson 11.6

Any valid data typeList of valid identifiers (any number)

Page 17: Chapter 11 – Pointer Variables. Declaring a Pointer Variable u Declared with data type, * and identifier type* pointer_variable; u * follows data type

Uses of typedef

Improve readability and understandability of the code

Easier to modify programs that are implementation dependent

Lesson 11.6

Page 18: Chapter 11 – Pointer Variables. Declaring a Pointer Variable u Declared with data type, * and identifier type* pointer_variable; u * follows data type

Arrays and typedef

General form: typedef type name [size];– type is the type of values in the array– name is the alias to be used as the data type in a

declaration– size is array size

Lesson 11.6

Page 19: Chapter 11 – Pointer Variables. Declaring a Pointer Variable u Declared with data type, * and identifier type* pointer_variable; u * follows data type

typedef for Pointer to Array

General form: typedef type (*name) [size];– type is type of values in array– name is the alias to be used as data type in

declaration– size is array size

Lesson 11.6

Page 20: Chapter 11 – Pointer Variables. Declaring a Pointer Variable u Declared with data type, * and identifier type* pointer_variable; u * follows data type

Returning a Pointer

Given the example: typedef double (*array_ptr) [2];– Creates alias array_ptr for declarations of

pointers to 1-D arrays of double with size 2 array_ptr function2 (argument);

– Indicates pointer to 1-D array is returned from function

Lesson 11.6

Page 21: Chapter 11 – Pointer Variables. Declaring a Pointer Variable u Declared with data type, * and identifier type* pointer_variable; u * follows data type

Multidimensional Arrays

C++ sees array of arrays Example: int a[2] [3] [4];

– Can work with whole (or 1) array– Can use 2 arrays of [3] [4]– Can use 6 arrays of [4]– Can use 24 integers– Pointers: int (*b)[3] [4], (*c)[4], *d, a

Can assign addresses with & operator ( b = &a[0]; )

Lesson 11.6

Page 22: Chapter 11 – Pointer Variables. Declaring a Pointer Variable u Declared with data type, * and identifier type* pointer_variable; u * follows data type

Pointers to Objects Special arrow operator ->

– Negative and greater than symbol with no space between

– Used to access members with object's address Declaring pointer Class_name* ptr_name; Initializing pointer ptr_name = &object_name; Calling member function (2 argument example)

ptr_name -> function_name (arg1, arg2)

Lesson 11.7

Page 23: Chapter 11 – Pointer Variables. Declaring a Pointer Variable u Declared with data type, * and identifier type* pointer_variable; u * follows data type

Pointers as Data Members

General form class Class_name { private: type* pointer_name; public: type function_name ( ); };

Lesson 11.8

Name of the class

Any valid data type including name of struct or class

Name of member function

Page 24: Chapter 11 – Pointer Variables. Declaring a Pointer Variable u Declared with data type, * and identifier type* pointer_variable; u * follows data type

Dynamic Memory Allocation

Optimize memory space with new and delete operators– Reserve and unreserve memory while program

is execution

– Pointer variables important because operators work with addresses of memory reserved

Lesson 11.9

Page 25: Chapter 11 – Pointer Variables. Declaring a Pointer Variable u Declared with data type, * and identifier type* pointer_variable; u * follows data type

new Operator

General form: new type [num_elements]– type is data type of array elements– num_elements is number of array elements

Reserves memory but does NOT fill with values– new returns address of memory it reserves– Value assigned to pointer variable of same type

Lesson 11.9

type* array_address; array_address = new type [num];

Page 26: Chapter 11 – Pointer Variables. Declaring a Pointer Variable u Declared with data type, * and identifier type* pointer_variable; u * follows data type

delete Operator

General form: delete [ ] array_address;– array_address is pointer variable

Releases memory stored at address indicated Knows size of memory reserved by new and

releases memory for all the array delete address;

– deletes memory for single element

Lesson 11.9

Page 27: Chapter 11 – Pointer Variables. Declaring a Pointer Variable u Declared with data type, * and identifier type* pointer_variable; u * follows data type

Summary

Declare and initialize pointer variables

Pass addresses to functions

Return an address from a function

Reserve memory during execution

Link classes with accessor functions

Learned how to: