1 / 45

CS-362: Data Structures Week 6 Part 2

CS-362: Data Structures Week 6 Part 2. Dr. Jesús Borrego. Topics. Dynamic Data Pointers Address Pointers to different data types Sample programs. Pointer Data Type/Pointer Variables. 325. The type is integer The Name of the integer is A The address is 10280

rhea-howe
Download Presentation

CS-362: Data Structures Week 6 Part 2

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. CS-362: Data StructuresWeek 6Part 2 Dr. Jesús Borrego

  2. Topics • Dynamic Data • Pointers • Address • Pointers to different data types • Sample programs

  3. Pointer Data Type/Pointer Variables 325 The type is integer The Name of the integer is A The address is 10280 The value stored in A is 325 int A = 325; 10280 A

  4. More Pointers

  5. Running the program

  6. Running the program

  7. PointerAndAddress

  8. Declaring Pointer Variables • Syntax: • Examples: int *p; char *ch; • These statements are equivalent: int *p; int* p; int * p;

  9. Pointer Intro Program (I)

  10. Pointer Intro Program (II)

  11. Running the program

  12. Pointer Intro Program (I)

  13. Pointer Intro Program (II)

  14. Pointer Intro Program (II)

  15. MoreStruct

  16. Running the program

  17. MoreBeatles (I)

  18. MoreBeatles (II)

  19. MoreBeatles (III)

  20. Running the program

  21. PointerSwap

  22. Running the Program

  23. What happened? The swap did not work? The before and after are the same Need to make sure we get the values back to the calling program It is calling by Value How do we fix it?

  24. Address of Operator (&) The ampersand, &, is called the address of operator The address of operator is a unary operator that returns the address of its operand

  25. PointerSwap2

  26. PointerSwap3

  27. Running the program (I)

  28. Running the program (II)

  29. stores 25 into the first memory location stores 35 into the second memory location Dynamic Arrays • Dynamic array: array created during the execution of a program • Example: int *p; p = new int[10]; *p = 25; p++; //to point to next array component *p = 35;

  30. Dynamic Arrays (cont'd.) • C++ allows us to use array notation to access these memory locations • The statements: p[0] = 25; p[1] = 35; store 25 and 35 into the first and second array components, respectively

  31. Dynamic Arrays (cont'd.)

  32. Dynamic Arrays (cont'd.)

  33. Dynamic Arrays (cont'd.) • The value of list (1000) is constant • Cannot be altered during program execution • The increment and decrement operations cannot be applied to list • If p is a pointer variable of type int, then: p = list; copies the value of list, the base address of the array, into p • We can perform ++ and -- operations on p • An array name is a constant pointer

  34. Dynamic Arrays (cont'd.)

  35. Functions and Pointers • A pointer variable can be passed as a parameter either by value or by reference • To make a pointer a reference parameter in a function heading, use &: void pointerParameters(int* &p, double *q) { . . . }

  36. Pointers and Function Return Values • A function can return a value of type pointer: int* testExp(...) { . . . }

  37. declares board to be an array of four pointers wherein each pointer is of type int creates the rows of board declares board to be a pointer to a pointer Dynamic Two-Dimensional Arrays • You can create dynamic multidimensional arrays • Examples:

  38. PointerArray

  39. PointerArray (Cont’d)

  40. Running the program

  41. PrintArray2D

  42. PrintArray2D (Cont’d)

  43. PrintArray (Cont’d)

  44. Running the Program

  45. Resources • Can search the internet for tutorials on pointers • On YouTube: • http://www.youtube.com/watch?v=eFWCQexfaeg • From Google, search for • “C++ pointer tutorial“

More Related