Lecture_12_Pointer.pptx

# Lecture_12_Pointer.pptx - Lecture 12 – Pointer...

• Notes
• 47

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

1 PROGRAMMING TECHNIQUE Lecture 12 – Pointer

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

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
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);

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

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.
Pointer Variables Pointer variables are yet another way using a memory address to work with a piece of data.

Subscribe to view the full document.

• Summer '16
• Mashanum
• Pointer, valptr

{[ snackBarMessage ]}

### What students are saying

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

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

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

Dana University of Pennsylvania ‘17, Course Hero Intern

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

Jill Tulane University ‘16, Course Hero Intern