Week 2 - Week 2 1 Chapter 3: Pointers and Reference...

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

View Full Document Right Arrow Icon
1 Week 2
Background image of page 1

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

View Full DocumentRight Arrow Icon
Chapter 3: Pointers and Reference Variables Outline: Pointers Pointer Arithmetic Memory Allocation Memory Leak Member Access Operators . and -> Reference Variables 2
Background image of page 2
3 Pointer Variables Pointer variables are variables that hold memory addresses of variables. In conjunction with pointer variables are two unary operators and . The unary operator , address of , gives the address of its operand. The unary operator . , dereferencing , gives the contents pointed at by its operand.
Background image of page 3

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

View Full DocumentRight Arrow Icon
4 Illustrating Pointer Variables $ cat ptrvar.cpp #include <iostream> using namespace std; int main() { int * p; int x = 12345; // int z; // uncomment it will change y’s location int y; p = x; cout << &p << ’\t’ << p << ’\t’ << *p << endl; p = y; cout << &p << ’\t’ << p << ’\t’ << *p << endl; y = 67890 ; cout << &p << ’\t’ << p << ’\t’ << *p << endl; } $ g++ ptrvar.cpp $ a.out 0xffbff89c 0xffbff898 12345 0xffbff89c 0xffbff894 -4196076 0xffbff89c 0xffbff894 67890
Background image of page 4
5 The meaning of int* x, y; The declaration int a,b; is the same as int a; int b; Consider the declaration int* x, y; It is the same as int *x; int y; It is clearer to place * just before the variable as int *x,y; so as to remind us that int is applicable to *x and y.
Background image of page 5

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

View Full DocumentRight Arrow Icon
6 Pointer Arithmetic An integer n can be added or subtracted from a pointer variable p pointing at variables of type theType. The result of p + n is p p + n × sizeof(theType)
Background image of page 6
7 Pointer Arithmetic Example $ cat parith.cpp #include <iostream> using namespace std; int main() { int *p, sum = 0; int a[] = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 }; for( p = a[0]; p <= a[9]; p++ ) sum += * p; // or for( p = a; p <= a + 9; p++ ) sum += *p; cout << sum << endl; } $ g++ parith.cpp $ a.out 45
Background image of page 7

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

View Full DocumentRight Arrow Icon
8 Arrays and pointers – (1) array name is a constant pointer that points to the zeroth array element char t1 [10]; char *pc1 = t1; //ok char t2 [10]; t1 = t2; // error pc1 = t1 + 5 // pc1 points to t1 [5] *t1 = *(t1 + 5) // assigns value of t1[5] to t1[0] 0 2 3 4 5 1 t1 pc1 pc1
Background image of page 8
9 Arrays and pointers – (2) #include <iostream> using namespace std; int main() { char t1 [] = "How are you?"; *t1 = *(t1 + 5); // same as t1[0] = t1[5] cout << "The character at t1[0] is " << t1[0] << endl; // r cout << "The modified string is: " << t1 << endl; // row are you? return 0; } $ g++ array1.cpp $ a.out The character at t1[0] is r The modified string is: row are you?
Background image of page 9

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

View Full DocumentRight Arrow Icon
10 Arrays and pointers – (3) If the index is out of range when we access an array, we will get garbage if we read from the entry, or we will encounter a runtime error some time later if we write to the entry Example: char temp = 'a'; int i = 7; char *c = temp; c ++ ; // Instead of using *c++ // c points to somewhere unintended
Background image of page 10
11 Array Access: index or pointer? #include <iostream>
Background image of page 11

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

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

Page1 / 66

Week 2 - Week 2 1 Chapter 3: Pointers and Reference...

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

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