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

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

1 Week 2

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

View Full Document
Chapter 3: Pointers and Reference Variables Outline: Pointers Pointer Arithmetic Memory Allocation Memory Leak Member Access Operators . and -> Reference Variables 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.

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

View Full Document
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 p = y; cout << &p << ’\t’ << p << ’\t’ << *p << endl; y = 67890 ; cout << &p } \$ g++ ptrvar.cpp \$ a.out 0xffbff89c 0xffbff898 12345 0xffbff89c 0xffbff894 -4196076 0xffbff89c 0xffbff894 67890
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.

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

View Full Document
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)
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

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

View Full Document
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
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?

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

View Full Document
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
11 Array Access: index or pointer? #include <iostream>

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

View Full Document
This is the end of the preview. Sign up to access the rest of the document.

{[ snackBarMessage ]}

### 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
Ask a homework question - tutors are online