Lecture_12_Pointer.pptx - Lecture 12 – Pointer...

This preview shows page 1 - 10 out of 47 pages.

1 PROGRAMMING TECHNIQUE Lecture 12 – Pointer
Image of page 1

Subscribe to view the full document.

Learning Outcomes At the end of this lecture, students should be able to: 1. Describe the concept of pointer. 2. Know how to do arithmetic operation using pointer. 3. Describe the relationship between pointer & array. 4. Use pointer as function parameter in program. 5. Understand the concept of dynamic memory allocation.
Image of page 2
Getting the Address of a Variable Each variable in program is stored at a unique address Use address operator & to get address of a variable: int num = -99; cout << &num; // prints address // in hexadecimal
Image of page 3

Subscribe to view the full document.

Pointer Variables Pointer variable : Often just called a pointer, it's a variable that holds an address Because a pointer variable holds the address of another piece of data, it "points" to the data
Image of page 4
Something Like Pointers: Arrays We have already worked with something similar to pointers, when we learned to pass arrays as arguments to functions. For example, suppose we use this statement to pass the array numbers to the showValues function: showValues(numbers, SIZE);
Image of page 5

Subscribe to view the full document.

Something Like Pointers : Arrays The values parameter, in the showValues function, points to the numbers array. C++ automatically stores the address of numbers in the values parameter .
Image of page 6
Something Like Pointers: Reference Variables We have also worked with something like pointers when we learned to use reference variables. Suppose we have this function: void getOrder(int &donuts) { cout << "How many doughnuts do you want? "; cin >> donuts; } And we call it with this code: int jellyDonuts; getOrder(jellyDonuts);
Image of page 7

Subscribe to view the full document.

Something Like Pointers: Reference Variables The donuts parameter, in the getOrder function, points to the jellyDonuts variable . C++ automatically stores the address of jellyDonuts in the donuts parameter.
Image of page 8
Pointer Variables Pointer variables are yet another way using a memory address to work with a piece of data.
Image of page 9

Subscribe to view the full document.

Image of page 10
  • Summer '16
  • Mashanum
  • Pointer, valptr

{[ snackBarMessage ]}

What students are saying

  • Left Quote Icon

    As a current student on this bumpy collegiate pathway, I stumbled upon Course Hero, where I can find study resources for nearly all my courses, get online help from tutors 24/7, and even share my old projects, papers, and lecture notes with other students.

    Student Picture

    Kiran Temple University Fox School of Business ‘17, Course Hero Intern

  • Left Quote Icon

    I cannot even describe how much Course Hero helped me this summer. It’s truly become something I can always rely on and help me. In the end, I was not only able to survive summer classes, but I was able to thrive thanks to Course Hero.

    Student Picture

    Dana University of Pennsylvania ‘17, Course Hero Intern

  • Left Quote Icon

    The ability to access any university’s resources through Course Hero proved invaluable in my case. I was behind on Tulane coursework and actually used UCLA’s materials to help me move forward and get everything together on time.

    Student Picture

    Jill Tulane University ‘16, Course Hero Intern