{[ promptMessage ]}

Bookmark it

{[ promptMessage ]}

Lecture7_8.bw - Lecture 7 Pointers ECE 3090 Software for...

Info icon This preview shows pages 1–12. Sign up to view the full content.

View Full Document Right Arrow Icon
ECE 3090 Software for Engineering Systems Dr. Ayanna Howard School of Electrical and Computer Engineering Georgia Institute of Technology Lecture 7 - Pointers
Image of page 1

Info icon This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document Right Arrow Icon
2 Adapted from “Brief Version of Starting Out with C++” 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 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 2
3 Adapted from “Brief Version of Starting Out with C++” 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 3

Info icon This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document Right Arrow Icon
4 Adapted from “Brief Version of Starting Out with C++” 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 4
5 Adapted from “Brief Version of Starting Out with C++” 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 5

Info icon This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document Right Arrow Icon
6 Adapted from “Brief Version of Starting Out with C++” 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 6
7 Adapted from “Brief Version of Starting Out with C++” Pointer Variables Pointer variables are yet another way using a memory address to work with a piece of data. Pointers are more "low-level" than arrays and reference variables. This means you are responsible for finding the address you want to store in the pointer and correctly using it.
Image of page 7

Info icon This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document Right Arrow Icon
8 Adapted from “Brief Version of Starting Out with C++” Pointer Variables Definition: int *intptr; Read as: intptr can hold the address of an int” Spacing in definition does not matter: int * intptr; // same as above int* intptr; // same as above
Image of page 8
9 Adapted from “Brief Version of Starting Out with C++” Pointer Variables Assigning an address to a pointer variable: int *intptr; intptr = &num; Memory layout: num intptr 25 0x4a00 address of num : 0x4a00
Image of page 9

Info icon This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document Right Arrow Icon
10 Adapted from “Brief Version of Starting Out with C++” Program 8-2
Image of page 10
11 Adapted from “Brief Version of Starting Out with C++” The Indirection Operator The indirection operator ( * ) dereferences a pointer.
Image of page 11

Info icon This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document Right Arrow Icon
Image of page 12
This is the end of the preview. Sign up to access the rest of the document.

{[ 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