{[ promptMessage ]}

Bookmark it

{[ promptMessage ]}

CarranoChap9and11 - Pointers Main Memory Byte#00000000...

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

View Full Document Right Arrow Icon
 Savitch -   CS 150 11 Pointers A pointer is the memory address of a variable. Main Memory Byte #00000000 Byte #00000001 Byte #00000010 Byte #00000011 Byte #00000100 Byte #00000101 Byte #00000110 Byte #00000111 Byte #00001000 Byte #00001001 Byte #00001010 Byte #00001011 Byte #00001100 Byte #00001101 Byte #00001110 Byte #00001111 01110101 11000101 10010100 00010000 11011110 00111010 11001000 00110000 00000000 00000000 00000001 00000101 00100100 11110100 10101001 11010010 Supposethat x is an int variablethat has been placed at this memory location (notethat it’s assumed that an int valuerequires 4 bytes of memory). In this example, x has thebinary value 00000000000000000000000100000101(i.e., 261) and is located at byte#8 (i.e., binary address 00001000). In theprogramthat uses variable x , thepointer to its memory location is accessed by using the & operator: &x is that pointer.
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
 Savitch -   CS 150 22 Pointer Variables A pointer can be stored in a variable. Main Memory Byte #00000000 Byte #00000001 Byte #00000010 Byte #00000011 Byte #00000100 Byte #00000101 Byte #00000110 Byte #00000111 Byte #00001000 Byte #00001001 Byte #00001010 Byte #00001011 Byte #00001100 Byte #00001101 Byte #00001110 Byte #00001111 01110101 11000101 10010100 00010000 11011110 00111010 11001000 00110000 00000000 00000000 00000001 00000101 00100100 11110100 10101001 11010010 If pointer variable p is declared as follows: int *p; Then p gets thememory address value(in this case00001000) and theprogramcan access theint valueat that address by using the * operator: *p is theint value(in this case261). Notethat pointers to different types of variables arenot interchangeable(e.g., if thefollowing declarations occur: int *p; float *q; then p and q areboth pointers but they are not the sametype of pointers!
Image of page 2
 Savitch -   CS 150 33 A Simple Example #include <iostream> using namespace std; void main() { int x = 25; int y = 99; int *p; // At this point, pointer p may be pointing to "illegal" memory! cout << "x = " << x << endl; cout << "&x = " << &x << endl; cout << "y = " << y << endl; cout << "&y = " << &y << endl; cout << "p = " << p << endl; cout << "*p = " << *p << endl; cout << endl; p = &y; // Now *p and y are located at the same place in memory. x = y; // Now x and y have the same value, at different locations. cout << "x = " << x << endl; cout << "&x = " << &x << endl; cout << "y = " << y << endl; cout << "&y = " << &y << endl; cout << "p = " << p << endl; cout << "*p = " << *p << endl; cout << endl; return; }
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
 Savitch -   CS 150 44 Another Simple Example #include <iostream> #include <iomanip> using namespace std; void main() { double *p, *q; double x = 1.357, y = -2.073; cout << " p = " << p << endl; cout << "*p = " << setw(10) << *p << endl; cout << " q = " << q << endl; cout << "*q = " << setw(10) << *q << endl; cout << endl; p = &x; q = &y; cout << setprecision(6); cout.setf(ios::fixed); cout << " p = " << p << endl; cout << "*p = " << setw(10) << *p << endl; cout << " q = " << q << endl; cout << "*q = " << setw(10) << *q << endl; cout << endl; x = 0.002; y = 9.999; cout << " p = " << p << endl; cout << "*p = " << setw(10) << *p << endl; cout << " q = " << q << endl; cout << "*q = " << setw(10) << *q << endl; cout << endl; *p = 5.432; *q = *p; cout << " p = " << p << endl; cout << "*p = " << setw(10) << *p << endl; cout << " q = " << q << endl; cout << "*q = " << setw(10) << *q << endl; cout << endl; *q = -8.246; p = q; cout << " p = " << p << endl; cout << "*p = " << setw(10) << *p << endl; cout << " q = " << q << endl; cout << "*q = " << setw(10) << *q << endl; cout << endl; return; }
Image of page 4
 Savitch -   CS 150 55 The new and delete Operators #include <iostream> using namespace std; void main() { int *ptr; cout << " ptr = " << ptr << endl; cout << "*ptr = " << *ptr << endl; cout << endl; ptr = new int; cout << " ptr = " << ptr << endl; cout << "*ptr = " << *ptr << endl; cout << endl; *ptr = 1776; cout << " ptr = " << ptr << endl; cout << "*ptr = " << *ptr << endl; cout << endl; delete ptr; cout << " ptr = " << ptr << endl; cout << "*ptr = " << *ptr << endl; cout << endl; return; } The new operator sets up spacein memory that is adequateto hold a dynamic variableof thedesignated type(in this case int ).
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
Image of page 6
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