Learning Objectives Pointers symbol and symbol Pointer operations
Learning Objectives ¨ Pointers ¨ * symbol and & symbol ¨ Pointer operations ¨ Pointer in function call ¨ Application: exchange values of two variables ¨ Pointer and array 1
Pointer Introduction ¨ Pointer definition: ¨ Memory address of a variable ¨ Recall: memory divided ¨ Numbered memory locations ¨ Variable name is used as address ¨ You’ve used memory address already! ¨ Call-by-reference ¨ Address of actual argument was passed ¨ Pass array in a function call 2
Pointer Variables ¨ Pointers are "typed" ¨ Can store addresses in variable ¨ Pointers 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! 3
Declaring Pointer Variables ¨ Pointers declared like other types ¨ Add "*" before variable name ¨ Produces "pointer to" that type ¨ "*" must be before each variable ¨ int *p 1, *p 2, v 1, v 2; ¨ p 1, p 2 hold pointers to int variables ¨ v 1, v 2 are ordinary int variables 4
Pointing to … ¨ int *p 1, *p 2, v 1, v 2; p 1 = &v 1; ¨ Sets pointer variable p 1 to "point to" int variable v 1 ¨ Operator, & ¨ Determines "address of" variable ¨ Read like: ¨ "p 1 equals address of v 1" ¨ Or "p 1 points to v 1" 5
Pointing to … ¨ Recall: int *p 1, *p 2, v 1, v 2; p 1 = &v 1; ¨ Two ways to refer to v 1 now: ¨ Variable v 1 itself: cout << v 1; ¨ Via pointer p 1: cout << *p 1; ¨ Dereference operator, * ¨ Pointer variable "dereferenced" ¨ Means: "Get data that p 1 points to" 6
"Pointing to" Example ¨ Consider: v 1 = 0; p 1 = &v 1; *p 1 = 42; cout << v 1 << endl; cout << *p 1 << endl; ¨ Produces output: 42 42 ¨ *p 1 and v 1 refer to same variable 7
& Operator ¨ The "address of" operator ¨ Also used to specify call-by-reference parameter ¨ Similar mechanism ¨ Call-by-reference parameters pass "address of" the actual argument 8
Pointer Assignments ¨ Pointer variables can be "assigned": int *p 1, *p 2; p 2 = p 1; ¨ Assigns one pointer to another ¨ "Make p 2 point to where p 1 points" ¨ Do not confuse with: *p 1 = *p 2; ¨ Assigns "value pointed to" by p 1, to "value pointed to" by p 2 9
Pointer Assignments Graphic: Display 10. 1 Uses of the Assignment Operator with Pointer Variables 10
Define Pointer Types ¨ Can "name" pointer types ¨ To be able to declare pointers like other variables ¨ Eliminate need for "*" in pointer declaration ¨ typedef int* Int. Ptr; ¨ Defines a "new type" alias ¨ Consider these declarations: Int. Ptr p; int *p; ¨ The two are equivalent 11
Pointers and Functions ¨ Recall ¨ Pass by value ¨ Pass by reference ¨ Pointers can be function parameters ¨ Behaves like pass by reference ¨ Example: next slide 12
void main() { int m = 77; int * p = &m; cout << "m = " << m << endl; sneaky(p); cout << "m = " << m << endl; } void sneaky(int * temp) { *temp = 99; } 13
Call-by-value Pointers Graphic: The Function Call sneaky(p); 14
Application of Pointer ¨ Exchange values of two variables ¨ Pass by value: won’t work ¨ Pass by reference: will work ¨ Pass by pointer to pointer: what is this? 15
Pointer and array ¨ Both array and pointer are pass by reference in a function call ¨ Both array and pointer refer to some physical memory address ¨ So, in C++, pointers can be used to represent arrays ¨ Any type of array (int, double, char, …) 16
Pointer and array (continued) double a[4] = {1. 1, 2. 2, 3. 3, 4. 4}; double f = 5. 5; double *p; p = &f; p = a; // or p = & a[0]; // now p can be used same as a to access array for(int i=0; i<4; i++) cout << p[i] << "t"; cout << endl; 17
Pointer and array (continued) ¨ So, we can use pointer to represent array, even in a function call double a[4] = {1. 1, 2. 2, 3. 3, 4. 4}; double *p = a; reset(p, 4); //or reset(a, 4); for(int i=0; i<4; i++) cout << p[i] << "t"; void reset(double * x, int size) { for(int i=0; i<size; i++) } x[i]=5. 5; 18
Pointer and C-String ¨ C-String is a character array, so pointer can be used to represent C-string ¨ What will be displayed? char str[] = "cs 201"; cout << str << endl; cout << "str contains " << strlen(str) << " characters" << endl; char *p; p = str; // p = &str[0]; cout << p << endl; cout << "p contains " << strlen(p) << " characters" << endl; 19
Pointer and C-String (continued) ¨ What should be displayed? char s 1[20] = "USA"; char s 2[20] = "Indiana“ char *p = s 1; void reset(char * x) char *q = s 2; { char s[20] = "IUSB"; q = p; reset(q); x=s; } cout << "q=" << q << endl; 20
Pointer and Array and C-String ¨ Assigment can not be performed in array but can be done in pointers int m[3] = {1, 2, 3}; int n[3] = {-1, -2, -3}; m = n; // This is NOT allowed int * p 1 = m; int * p 2 = n; p 1 = p 2; // This is allowed 21
Pointer and Array and C-String (continued) ¨ Assigment can not be performed in C-string but can be done in pointers char m[] = “Hello”; char n[] = “South Bend”; m = n; // This is NOT allowed char * p 1 = m; char * p 2 = n; p 1 = p 2; // This is allowed 22
- Slides: 22