CarranoChap9and11 - Savitch - CS 150 Savitch - CS 150 11...

Info iconThis preview shows pages 1–5. Sign up to view the full content.

View Full Document Right Arrow Icon

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

View Full DocumentRight Arrow Icon

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

View Full DocumentRight Arrow Icon
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: Savitch - CS 150 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 Suppose that x is an int variable that has been placed at this memory location (note that it’s assumed that an int value requires 4 bytes of memory). In this example, x has the binary value 00000000000000000000000100000101(i.e., 261) and is located at byte #8 (i.e., binary address 00001000). In the program that uses variable x , the pointer to its memory location is accessed by using the & operator: &x is that pointer. Savitch - CS 150 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 the memory address value (in this case 00001000) and the program can access the int value at that address by using the * operator: *p is the int value (in this case 261). Note that pointers to different types of variables are not interchangeable (e.g., if the following declarations occur: int *p; float *q; then p and q are both pointers but they are not the same type of pointers! Savitch - CS 150 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; } Savitch - CS 150 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;...
View Full Document

This note was uploaded on 08/26/2009 for the course CS 150 taught by Professor Klein during the Spring '08 term at Southern Illinois University Edwardsville.

Page1 / 20

CarranoChap9and11 - Savitch - CS 150 Savitch - CS 150 11...

This preview shows document pages 1 - 5. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online