210 likes | 363 Views
This chapter focuses on pointer variables in C++, essential for addressing memory and indirect referencing. It explains the difference between direct variable references and pointers that hold memory addresses. Key topics include pointer variable declarations, initialization, operators, and interactions with arrays. Special attention is given to pointer arithmetic, reference passing in functions, and the use of the `sizeof` operator to determine memory sizes. This foundational knowledge is crucial for efficient memory management and manipulation in C++ programming.
E N D
Chapter 8Pointers C++, How to Program Deitel & Deitel CSRU1600 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; CSRU1600 Yanjun Li
Variable and Pointer Variable • Indirection • Referencing value through pointer CSRU1600 Yanjun Li
Pointer Variable Initialization (1) • 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; CSRU1600 Yanjun Li
Pointer Variable Initialization (2) • 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. CSRU1600 Yanjun Li
Pointer Operators • Indirection/Dereferenceing operator(*) • Returns synonym for the object its operand points to • *yPtrreturnsy (becauseyPtrpoints toy) *yptr = 9; -> y = 9; • 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 CSRU1600 Yanjun Li
Pointer Types int x; x = 12; int* ptr; ptr = &x; std::cout << *ptr; *ptr is the value in the place to which ptr points 2000 12 x 3000 2000 ptr CSRU1600 Yanjun Li
Pointer Types int x; x = 12; int* ptr; ptr = &x; *ptr = 5; // changes the value // at adddress ptr to 5 2000 12 5 x 3000 2000 ptr CSRU1600 Yanjun Li
Pointer Types 4000 A Z ch 5000 6000 4000 4000 q p 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 CSRU1600 Yanjun Li
Pointers and Arrays (1) • 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. CSRU1600 Yanjun Li
Pointers and Arrays (2) • Accessing array elements with pointers • Assume declarations: int b[ 5 ];int *bPtr; bPtr = b; • Element b[ n ] can be accessed by *( bPtr + n ) • Called pointer/offset notation • Addresses • &b[ 3 ] is same as bPtr + 3 • Array name can be treated as pointer • b[ 3 ] is same as*( b + 3 ) • Pointers can be subscripted (pointer/subscript notation) • bPtr[ 3 ] is same as b[ 3 ] CSRU1600 Yanjun Li
Pointer Arithmetic (1) • Pointer arithmetic • Increment/decrement pointer (++ or --) • Add/subtract an integer to/from a pointer (+ or +=,- or -=) • Pointers may be subtracted from each other • Pointer arithmetic is meaningless unless performed on a pointer to an array CSRU1600 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 CSRU1600 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. CSRU1600 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 CSRU1600 Yanjun Li
sizeofoperator (1) • Returns size of operand in bytes • For arrays, sizeof returns ( size of 1 element ) * ( number of elements ) • If sizeof( int ) returns 4thenint myArray[ 10 ]; cout << sizeof( myArray ); will print 40 • Can be used with • Variable names • Type names • Constant values CSRU1600 Yanjun Li
sizeofoperator (2) • Using the sizeof operator in a function to find the size in bytes of an array parameter results in the size in bytes of a pointer, not the size in bytes of the array. • Is performed at compiler-time • For double realArray[ 22 ]; • Use sizeof realArray / sizeof( double ) to calculate the number of elements in realArray • Parentheses are only required if the operand is a type name CSRU1600 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. CSRU1600 Yanjun Li