180 likes | 273 Views
Introduction to Pointers Lesson xx. Objectives. Memory setup Pointer declaration Address operator Indirection Printing addresses or pointers. Memory Setup. 10. a. (1ab). int a = 10; float x = 17.24; char c = ‘q’;. 17.24. (2fe). x. ‘q’. c. (3cd). Address . 10. a. (1ab).
E N D
Objectives • Memory setup • Pointer declaration • Address operator • Indirection • Printing addresses or pointers
Memory Setup 10 a (1ab) int a = 10; float x = 17.24; char c = ‘q’; 17.24 (2fe) x ‘q’ c (3cd)
Address 10 a (1ab) int a = 10; float x = 17.24; char c = ‘q’; 17.24 (2fe) x ‘q’ c (3cd) 5fb
Pointer Declaration 10 a (1ab) int a = 10; float x = 17.24; char c = ‘q’; int *ptr; 17.24 (2fe) x ‘q’ c (3cd) ptr
Address Operator & 10 a (1ab) int a = 10; float x = 17.24; char c = ‘q’; cout << a; cout << &a; 17.24 (2fe) x ‘q’ c (3cd)
Initializing a Pointer Variable 10 a (1ab) int a = 10; float x = 17.24; char c = ‘q’; int *ptr; ptr = &a; 17.24 (2fe) x ‘q’ c (3cd) 1ab ptr
Different Interpretation 10 a (1ab) int a = 10; float x = 17.24; char c = ‘q’; int *ptr; ptr = &a; 17.24 (2fe) x ‘q’ c (3cd) 1ab ptr
Redrawing a 1ab 10 (1ab) ptr int a = 10; float x = 17.24; char c = ‘q’; int *ptr; ptr = &a; 17.24 (2fe) x ‘q’ c (3cd)
Sample Pointer Program #include <iostream> using std::cout; using std::endl; int main() { int y, cc = 10; int* intptr; intptr = &cc; y = *intptr; *intptr= 32;cout << y <<* intptr << intptr <<& intptr << endl; return 0; }
Setting up Variables int y, cc = 10; int* intptr; intptr = &cc; y = *intptr; *intptr= 32; cout << y <<* intptr << intptr <<& intptr << endl; y (1ab) 10 cc (4fe)
Setting up a Pointer int y, cc = 10; int* intptr; intptr = &cc; y = *intptr; *intptr= 32;cout << y <<*intptr << ptr <<&intptr << endl; ; y (1ab) intptr 10 cc (4fe)
Initializing a Pointer int y, cc = 10; int* intptr; intptr = &cc; y = *intptr; *intptr= 32; cout << y <<*intptr <<intptr <<&intptr << endl; y (1ab) 4fe intptr 10 cc (4fe)
Indirection on the Right int y, cc = 10; int* intptr; intptr = &cc; y = *intptr; *intptr= 32;cout << y <<* intptr << intptr <<& intptr << endl; 10 y (1ab) 4fe intptr 10 cc (4fe)
Indirection on the Left int y, cc = 10; int* intptr; intptr = &cc;y = *intptr; *intptr= 32;cout << y <<*intptr << intptr <<& intptr << endl; 10 y (1ab) 4fe intptr 32 cc (4fe)
Address of a Pointer Variable int y, cc = 10; int* intptr; intptr = &cc; y = *intptr; cout << y <<*intptr <<intptr<<&intptr << endl; 10 y (1ab) 4fe intptr (55a) 32 cc (4fe)
Printing Pointers int y, cc = 10; int* intptr; intptr = &cc; y = *intptr; cout << y <<*intptr <<intptr <<&intptr << endl; 10 y (1ab) 4fe intptr (55a) 32 cc (4fe) Output 10 32 4fe 55a
Summary • Memory setup • Pointer declaration • Address operator • Indirection • Printing addresses or pointers