1 / 22

Introduction to Pointers in C++

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.

garzah
Download Presentation

Introduction to Pointers in C++

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Learning Objectives • Pointers • * symbol and & symbol • Pointer operations • Pointer in function call • Application: exchange values of two variables • Pointer and array

  2. 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

  3. 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!

  4. 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

  5. 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"

  6. 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"

  7. "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

  8. & Operator • The "address of" operator • Also used to specify call-by-referenceparameter • Similar mechanism • Call-by-reference parameters pass"address of" the actual argument

  9. 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

  10. Pointer Assignments Graphic: Display 10.1 Uses of the Assignment Operator with Pointer Variables

  11. 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

  12. Pointers and Functions • Recall • Pass by value • Pass by reference • Pointers can be function parameters • Behaves like pass by reference • Example: next slide

  13. void main() { int m = 77; int * p = &m; cout << "m = " << m << endl; sneaky(p); cout << "m = " << m << endl; } void sneaky(int * temp) { *temp = 99; }

  14. Call-by-value Pointers Graphic: The Function Call sneaky(p);

  15. 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?

  16. 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, …)

  17. 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;

  18. 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; }

  19. 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;

  20. 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; }

  21. 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

  22. 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

More Related