180 likes | 341 Views
Chapter 8 Pointers. C++, How to Program Deitel & Deitel. 1. Pointer Variable Declarations. Pointer variables contain memory addresses as values Normally, variable contains specific value (direct reference)
E N D
Chapter 8Pointers C++, How to Program Deitel & Deitel 1 CISC 1600 Yanjun Li
Pointer Variable Declarations Pointer variables contain memory addresses as values Normally, variable contains specific value (direct reference) Pointers contain address of variable that has specific value (indirect reference) Example : int *countPtr; //a pointer to int int count; //int variable //pointer variables to double double *xPtr, *yPtr; 2 CISC 1600 Yanjun Li
Pointer Variable Initialization (1) Initialized to an address Using the address operator (&) Example int y = 5; //int variable y int *yPtr; //declare a pointer yPtr to int yPtr = &y; //and assign the address of y to yPtr. 3 CISC 1600 Yanjun Li
Pointer Variable Initialization (2) Initialized to 0, NULL 0 or NULL points to nothing (null pointer) 0 is the only integer value that can be assigned directly to a pointer variable without casting the integer to a pointer type first. Example: int * countPtr; countPtr = 0; 4 CISC 1600 Yanjun Li
Pointer Operators Indirection/Dereferenceing operator(*) Returns synonym for the object its operand points to *yPtrreturnsy (becauseyPtrpoints toy) *yptr = 5; -> y = 5; An attempt to dereference a variable that is not a pointer is a compilation error. * and & are inverses of each other *&y -> y &*yPtr -> yPtr 5 CISC 1600 Yanjun Li
int x; x = 12; int* ptr; ptr = &x; std::cout << *ptr; *ptr is the value in the place to which ptr points Work with Pointer 2000 12 x 3000 2000 ptr 6 CISC 1600 Yanjun Li
int x; x = 12; int* ptr; ptr = &x; *ptr = 5; // changes the value // at adddress ptr to 5 Work with Pointer 2000 12 5 x 3000 2000 ptr 7 CISC 1600 Yanjun Li
char ch; ch = ‘A’; char* q; q = &ch; *q = ‘Z’; char* p; p = q; // the right side has value 4000 // now p and q both point to ch Work with Pointer 4000 A Z ch 5000 6000 4000 4000 q p 8 CISC 1600 Yanjun Li
Pointers and Arrays Arrays and pointers are closely related Array name is like constant pointer The value saved in an array name is the address of the first element of the array. We can use pointer as the array name. 9 CISC 1600 Yanjun Li
Pointer Assignment Pointer can be assigned to another pointer if both are of same type Example int area =1; double *pArea = &area; //wrong 10 CISC 1600 Yanjun Li
Passing Arguments to Functions by Reference with Pointers Three ways to pass arguments to a function Pass-by-value Pass-by-reference with reference arguments Pass-by-reference with pointer arguments A function can return only zero or one value explicitly. Arguments passed to a function using reference arguments Function can modify original values of arguments More than one value “returned” implicitly. 11 CISC 1600 Yanjun Li
Pass-by-reference With Pointer Arguments Simulates pass-by-reference Use pointers and indirection operator Pass address of argument using & operator * operator used as alias/nickname for variable inside of function Example void squareByReference(int *); //function prototype //function declaration void squareByReference(int *nPtr) { *nPtr = *nPtr * *nPtr; } int x; squareByReference(&x); //call this function 12 CISC 1600 Yanjun Li
Reference Reproduced from the Cyber Classroom for C++, How to Program, 5/e by Deitel & Deitel. Reproduced by permission of Pearson Education, Inc. Fall 2008 13 CISC 1600 Yanjun Li CSRU1600 Yanjun Li