220 likes | 234 Views
Learn the basics of pointers in C++, including pointer variables, pointer assignments, and their applications in exchanging values of two variables, as well as pointers and arrays.
E N D
Learning Objectives • Pointers • * symbol and & symbol • Pointer operations • Pointer in function call • Application: exchange values of two variables • Pointer and array
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
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!
Declaring Pointer Variables • Pointers declared like other types • Add "*" before variable name • Produces "pointer to" that type • "*" must be before each variable • int *p1, *p2, v1, v2; • p1, p2 hold pointers to int variables • v1, v2 are ordinary int variables
Pointing to … • int *p1, *p2, v1, v2;p1 = &v1; • Sets pointer variable p1 to "point to" int variable v1 • Operator, & • Determines "address of" variable • Read like: • "p1 equals address of v1" • Or "p1 points to v1"
Pointing to … • Recall:int *p1, *p2, v1, v2;p1 = &v1; • Two ways to refer to v1 now: • Variable v1 itself:cout << v1; • Via pointer p1:cout << *p1; • Dereference operator, * • Pointer variable "dereferenced" • Means: "Get data that p1 points to"
"Pointing to" Example • Consider:v1 = 0;p1 = &v1;*p1 = 42;cout << v1 << endl;cout << *p1 << endl; • Produces output:4242 • *p1 and v1 refer to same variable
& Operator • The "address of" operator • Also used to specify call-by-referenceparameter • Similar mechanism • Call-by-reference parameters pass"address of" the actual argument
Pointer Assignments • Pointer variables can be "assigned":int *p1, *p2;p2 = p1; • Assigns one pointer to another • "Make p2 point to where p1 points" • Do not confuse with:*p1 = *p2; • Assigns "value pointed to" by p1, to "valuepointed to" by p2
Pointer Assignments Graphic: Display 10.1 Uses of the Assignment Operator with Pointer Variables
Define Pointer Types • Can "name" pointer types • To be able to declare pointers like othervariables • Eliminate need for "*" in pointer declaration • typedef int* IntPtr; • Defines a "new type" alias • Consider these declarations:IntPtr p;int *p; • The two are equivalent
Pointers and Functions • Recall • Pass by value • Pass by reference • Pointers can be function parameters • Behaves like pass by reference • Example: next slide
void main() { int m = 77; int * p = &m; cout << "m = " << m << endl; sneaky(p); cout << "m = " << m << endl; } void sneaky(int * temp) { *temp = 99; }
Call-by-value Pointers Graphic: The Function Call sneaky(p);
Application of Pointer Exchange values of two variables • Pass by value: won’t work • Pass by reference: will work • Pass by pointer: will work • Pass by pointer to pointer: what is this?
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, …)
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;
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; }
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[] = "cs201"; cout << str << endl; cout << "strcontains " << strlen(str) << " characters" << endl; char *p; p = str; // p = &str[0]; cout << p << endl; cout << "p contains " << strlen(p) << " characters" << endl;
Pointer and C-String (continued) • What should be displayed? char s1[20] = "USA"; char s2[20] = "Indiana“ char *p = s1; char *q = s2; q = p; reset(q); cout << "q=" << q << endl; void reset(char * x) { char s[20] = "IUSB"; x=s; }
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 * p1 = m; int * p2 = n; p1 = p2; // This is allowed
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 * p1 = m; char * p2 = n; p1 = p2; // This is allowed