Presentation is loading. Please wait.

Presentation is loading. Please wait.

C++ for Engineers and Scientists Second Edition Chapter 12 Pointers.

Similar presentations


Presentation on theme: "C++ for Engineers and Scientists Second Edition Chapter 12 Pointers."— Presentation transcript:

1 C++ for Engineers and Scientists Second Edition Chapter 12 Pointers

2 C++ for Engineers and Scientists, Second Edition2 Objectives Addresses and Pointers Array Names as Pointers Pointer Arithmetic Passing Addresses Common Programming Errors

3 C++ for Engineers and Scientists, Second Edition3 Addresses and Pointers Address operator & placed in front of a variable provides the memory address of the variable

4 C++ for Engineers and Scientists, Second Edition4 Addresses and Pointers (continued) Pointer variable: used to store the address of another variable Indirection operator ( * ): when followed by a pointer, it means “the variable whose address is stored in” Example: numAddr = &num // numAddr contains address // of num *numAddr // numAddr points to num

5 C++ for Engineers and Scientists, Second Edition5 Addresses and Pointers (continued) To declare a pointer, specify the type of the variable to which it will point Syntax: dataType *pointerName; Example: int *numAddr;

6 C++ for Engineers and Scientists, Second Edition6 Addresses and Pointers (continued)

7 C++ for Engineers and Scientists, Second Edition7 Addresses and Pointers (continued) Declaration of a pointer includes the type of variable it points to; indicates how much storage to retrieve

8 C++ for Engineers and Scientists, Second Edition8 Addresses and Pointers (continued) References vs. pointers: –Reference is a named constant for an address and cannot be modified –Pointer is a variable and can be modified –References are said to be automatically dereferenced or implicitly dereferenced –Pointers must be explicitly dereferenced using the dereferencing operator *

9 C++ for Engineers and Scientists, Second Edition9 Addresses and Pointers (continued) To pass a simple variable as a function argument, use a reference When dynamic memory allocation is required, use pointers References: can be used as formal function parameters and function return types, and also as variables Reference variable: a new name for an existing variable (both point to the same location); also called an alias

10 C++ for Engineers and Scientists, Second Edition10 Addresses and Pointers (continued) Syntax: dataType& aliasName = variableName;

11 C++ for Engineers and Scientists, Second Edition11 Addresses and Pointers (continued) Original variable or its alias can be used Reference must be declared with same data type as original variable Reference cannot be equated with a constant Reference cannot be changed to point to another variable Multiple references can be declared on the same line; each must be preceded by &

12 C++ for Engineers and Scientists, Second Edition12 Addresses and Pointers (continued) Compiler automatically uses the variable when a new value is assigned to the reference Example: int b: // original variable int& a=b; // a is a reference variable // with b’s address a = 10; // change b’s value to 10 Compiler automatically performs an indirect access of b ’s value without explicitly using indirection symbol * ; called automatic dereference

13 C++ for Engineers and Scientists, Second Edition13 Addresses and Pointers (continued) Same example with a pointer: int b;// declare integer variable int *a = &b; // declare pointer a, // store b’s address *a = 10;// changes b’s value to 10 This example uses explicit dereference Pointer can be changed to point to another variable Indirection operator * must be used; also called dereferencing operator

14 C++ for Engineers and Scientists, Second Edition14 Array Names as Pointers Internally, the compiler uses addresses to access specific array elements Example: integer array grade To call grade [3], the compiler computes: &grade[3] = &grade[0] + (3 * sizeof(int))

15 C++ for Engineers and Scientists, Second Edition15 Array Names as Pointers (continued)

16 C++ for Engineers and Scientists, Second Edition16 Array Names as Pointers (continued) Array elements may be referenced in two ways:

17 C++ for Engineers and Scientists, Second Edition17 Array Names as Pointers (continued)

18 C++ for Engineers and Scientists, Second Edition18 Array Names as Pointers (continued) When an array is created, the array name becomes the name of a pointer constant containing the address of the first location in the array

19 C++ for Engineers and Scientists, Second Edition19 Array Names as Pointers (continued)

20 C++ for Engineers and Scientists, Second Edition20 Array Names as Pointers (continued) Array name and pointer can be used interchangeably in most respects, except: –True pointer is a variable, and its contents (an address) can be changed –Array name is a pointer constant, and its contents (an address) cannot be changed Array declared with size causes static allocation of memory for all the elements, whether used or not

21 C++ for Engineers and Scientists, Second Edition21 Array Names as Pointers (continued) Dynamic allocation allows the allocated memory to be determined and adjusted at run time, using new and delete operators Explicit dynamic requests are made in a declaration or assignment statement

22 C++ for Engineers and Scientists, Second Edition22 Array Names as Pointers (continued)

23 C++ for Engineers and Scientists, Second Edition23 Pointer Arithmetic Arithmetic operations can be done on pointer variables (as long as a valid address results) For arrays, the compiler will scale the arithmetic operation to ensure that the result points to a value of the correct type Addresses can be incremented or decremented using prefix and postfix increment and decrement operators

24 C++ for Engineers and Scientists, Second Edition24 Pointer Arithmetic (continued)

25 C++ for Engineers and Scientists, Second Edition25 Pointer Arithmetic (continued) Relational operators can be used to compare addresses in pointers

26 C++ for Engineers and Scientists, Second Edition26 Pointer Arithmetic (continued) Pointers can be initialized when declared If pointing to a variable, the variable must be declared first Pointers to arrays can be initialized when declared in two ways Example: double volts[5];// declare array double *zing = &volts[0];// first way double *zing = volts; // second way

27 C++ for Engineers and Scientists, Second Edition27 Passing Addresses Addresses can be implicitly passed to functions using reference variables Addresses can be explicitly passed to functions using pointers; this is a pass by reference Place the address operator & in front of the variable to pass its address Example: swap(&firstnum, &secnum)

28 C++ for Engineers and Scientists, Second Edition28 Passing Addresses (continued) Function must be created with pointer arguments Example: void swap(double *nm1Addr, double *nm2Addr); When an array is passed to a function, only its address is actually passed (an entire array is always passed by reference) The address of the array is also the address of element 0

29 C++ for Engineers and Scientists, Second Edition29 Passing Addresses (continued)

30 C++ for Engineers and Scientists, Second Edition30 Passing Addresses (continued)

31 C++ for Engineers and Scientists, Second Edition31 Passing Addresses (continued) Pointer notation can be used for multidimensional arrays, but it becomes complex as the array dimensions increase

32 C++ for Engineers and Scientists, Second Edition32 Passing Addresses (continued) An equivalent set of relationships:

33 C++ for Engineers and Scientists, Second Edition33 Passing Addresses (continued) Function name is also a pointer Pointer can be declared to point to a function name

34 C++ for Engineers and Scientists, Second Edition34 Common Programming Errors Attempting to store an address in a non-pointer variable Using a pointer to access non-existent array elements (out of bounds) Failing to use the bracket set [] after the delete operator when dynamically deallocating memory previously allocated by the new[] operator Incorrectly applying the address and indirection operators

35 C++ for Engineers and Scientists, Second Edition35 Common Programming Errors (continued) Taking the address of a pointer constant (which is already an address) Taking the address of a reference argument, reference variable, or register variable (which are already addresses) Initializing pointer variables incorrectly Failing to understand if a variable contains an address or is an address

36 C++ for Engineers and Scientists, Second Edition36Summary Address of a variable can be obtained using the address operator & Pointer variable stores the address of another variable Array name is a pointer constant Access to an array element using subscript notation can be replaced using pointer notation Arrays can be dynamically created at run time

37 C++ for Engineers and Scientists, Second Edition37 Summary (continued) Arrays are passed to functions as addresses When passing a single-dimensional array to a function, the function argument can be declared as a pointer or as an array Pointers can be incremented, decremented, compared, and assigned Values added to or subtracted from a pointer are automatically scaled based on the data type


Download ppt "C++ for Engineers and Scientists Second Edition Chapter 12 Pointers."

Similar presentations


Ads by Google