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

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

View Full Document Right Arrow Icon
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) cout << sizeof (&j) cout << sizeof (&c) 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) cout << "size of long Addr: " << sizeof (&j) cout << "size of float Addr: " << sizeof (&f) cout << "Address of i: " << &i cout << "Address of j: " << &j cout << "Address of f: " << &f return 0; } Page 1 of 12
Background image of page 1

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

View Full DocumentRight Arrow Icon
#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 cout << "j's Addr: " << &j cout << "f's Addr: " << &f 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
Background image of page 2
Image of page 3
This is the end of the preview. Sign up to access the rest of the document.

This note was uploaded on 12/07/2010 for the course CS 175 taught by Professor Mike during the Spring '06 term at Golden West College.

Page1 / 12

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

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

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