Session_6 - Lesson 8 Pointers A pointer is a variable that...

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

Lesson 8 – Pointers A pointer is a variable that holds a memory address. To get the address of a variable, you use the address-of operator (&) which returns the address of an object in memory. An address is always 4 byte long (on most compilers), no matter what data type it is pointing at (short, long, char, …) #include <iostream> using namespace std; int main() { short int i; long int j; char c; cout << sizeof (&i) << endl; cout << sizeof (&j) << endl; cout << sizeof (&c) << endl; return 0; } Each variable is located at a unique location in memory. By default, addresses are displayed in hexadecimal code. #include <iostream> using namespace std; int main() { short int i = 1; long int j = 2; float f = 3.14; cout << "size of short int Addr: " << sizeof (&i) << endl; cout << "size of long Addr: " << sizeof (&j) << endl; cout << "size of float Addr: " << sizeof (&f) << endl; cout << "Address of i: " << &i << endl; cout << "Address of j: " << &j << endl; cout << "Address of f: " << &f << endl; return 0; } Page 1 of 12
Image of page 1

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

Storing the Address in a Pointer #include <iostream> using namespace std; int main() { short int *ptr_i; // declares ptr_i to be a pointer to a short int long int *ptr_j; float *ptr_f; short int i = 1; // declares i to be a short int long j = 2; float f = 3; ptr_i = &i; // puts i's address in ptr_i ptr_j = &j; ptr_f = &f; cout << "i's Addr: " << &i << endl; cout << "j's Addr: " << &j << endl; cout << "f's Addr: " << &f << endl; cout << "Address of i stored in ptr_i: " << ptr_i << endl; cout << "Address of j stored in ptr_j: " << ptr_j << endl; cout << "Address of f stored in ptr_f: " << ptr_f << endl; return 0; } The expression int *pAge = 0; declares pAge to be a pointer to int. That is, pAge is declared to hold the address of an int. When you declare an integer variable (type int) it is set up to hold an integer. When you declare a pointer variable it is set up to hold an address. So pAge is a different type of variable than int but it points to int types. unsigned short int howOld = 50; unsigned short int * pAge = 0; pAge = &howOld; The first line creates a variable—howOld, whose type is unsigned short int and initializes it with the value 50. The second line declares pAge to be a pointer to type unsigned short int and initializes it to zero. You know that pAge is a pointer because of the asterisk after the variable type and before the variable name. The third and final line assigns the address of howOld to the pointer pAge. You can tell that the address of howOld is being assigned because of the address-of operator (&). If the address-of operator had not been used, the value of howOld would have been assigned, which may or may not be a valid address. Page 2 of 12
Image of page 2
You can also accomplish this in one step with unsigned short int howOld = 50; // make a variable unsigned short int * pAge = &howOld; // make pointer to howOld pAge is a pointer that now contains the address of the howOld variable. Using pAge, you can actually determine the value of howOld, which in this case is 50. Accessing howOld by using the pointer page is called indirection because you are indirectly accessing howOld by means of pAge.
Image of page 3

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

Image of page 4
This is the end of the preview. Sign up to access the rest of the document.
  • Fall '06
  • Mike
  • Pointer, pPointer

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