pointers. contents zbasics of pointer zarray and pointer yusing array and pointer interchangeably...

35
Pointers

Upload: giles-mcdonald

Post on 03-Jan-2016

281 views

Category:

Documents


12 download

TRANSCRIPT

Page 1: Pointers. Contents zBasics of Pointer zArray and pointer yusing array and pointer interchangeably yPassing array(pointer) to functions zDynamic memory

Pointers

Page 2: Pointers. Contents zBasics of Pointer zArray and pointer yusing array and pointer interchangeably yPassing array(pointer) to functions zDynamic memory

Contents

Basics of PointerArray and pointer

using array and pointer interchangeably Passing array(pointer) to functions

Dynamic memory allocationString and pointerReference vs. pointer

Page 3: Pointers. Contents zBasics of Pointer zArray and pointer yusing array and pointer interchangeably yPassing array(pointer) to functions zDynamic memory

Pointer

- variables storing memory address

Intimately tied to array and stringOperators

*: deference or indirection operator, e.g. *p &: address operator, e.g. &x

Page 4: Pointers. Contents zBasics of Pointer zArray and pointer yusing array and pointer interchangeably yPassing array(pointer) to functions zDynamic memory

Pointer: Declaration and Usage

void main() {

int i = 5;

// Q: 想儲存 i的位址 ? How?

// A: 宣告一個指標 pint *p = &i ;

// Q: 如何利用 p取出 i的值 ?

// A: 使用 * operatorcout << *p ;

// Q: 我可以印出 i的位址 (p的值 )?

// A: it’s OK!, 如下 :

cout << p;

}

Page 5: Pointers. Contents zBasics of Pointer zArray and pointer yusing array and pointer interchangeably yPassing array(pointer) to functions zDynamic memory

Pointer: Declaration and Usage

void main() {

int i=5, j=6;

int *p = &i ;

// Q: 可以利用 p 來改變 i 的值 ?

// A: yes

*p = *p + 12;

// A: 此外, p 也可以改存其它變數的位址p = &j ;

j = *p + 2; cout << j;

}

Page 6: Pointers. Contents zBasics of Pointer zArray and pointer yusing array and pointer interchangeably yPassing array(pointer) to functions zDynamic memory

Pointer vs. Array

在 C/C++ 程式寫作上, Array和 Pointer 好像雙 生兄弟,經常交換使用。

Page 7: Pointers. Contents zBasics of Pointer zArray and pointer yusing array and pointer interchangeably yPassing array(pointer) to functions zDynamic memory

Array Name Pointer to First Element

main() {

int a[5] = {1,2,3,4,5};

cout << &a[0];

cout << a ;

// 既然 a 記錄 a[0] 的位址,我就可以 ...

cout << *a; // 印出 a[0]

cout << *(a+1) ; // 印出 a[1]

*(a+2) = 0;

}

Page 8: Pointers. Contents zBasics of Pointer zArray and pointer yusing array and pointer interchangeably yPassing array(pointer) to functions zDynamic memory

Array Name Constant Pointer

main() {

int a[5] = {1,2,3,4,5};

int *p;

p = a ; // p = &a[0]

// 利用 p 印出 a[] 的內容for (int i=0;i<=4; i++)

{ cout << *p; p++;}

for (int i=0;i<=4; i++)

{ cout << *a; a++;}

}

Page 9: Pointers. Contents zBasics of Pointer zArray and pointer yusing array and pointer interchangeably yPassing array(pointer) to functions zDynamic memory

Insight Pointer Arithmetic

1

600

a[0] a[1] a[2] a[3] a[4]

Addr

2 3 4 5

604 608 612 616

int a[5] = {1,2,3,4,5};p = a ;p++ ; p+= 2;

p

char a[5] = {‘a’,’b’,’c’,’d’};p = a ; p+= 3;cout << *p;

a

600

a[0] a[1] a[2] a[3] a[4]

Addr

b c d

601 602 603 604

p

p p

Page 10: Pointers. Contents zBasics of Pointer zArray and pointer yusing array and pointer interchangeably yPassing array(pointer) to functions zDynamic memory

Using Array and Pointer Interchangeably

main() {

int a[size] = {1,2,3,4,5};

int *p, i ;

for (p=a, i=0 ;i<size; i++)

cout << *p++;

for (p=a, i=0 ; i<size; i++)

cout << p[i];

}

Page 11: Pointers. Contents zBasics of Pointer zArray and pointer yusing array and pointer interchangeably yPassing array(pointer) to functions zDynamic memory

Passing Array to Function

const int size = 5 ;

main() {

int a[size] = {1,2,3,4,5};

arr_mul2(a, size); // a &a[0]}

void arr_mul2(int* p, int n) {

for(int i = 0; i<n; i++)

p[i] *= 2;

}

Page 12: Pointers. Contents zBasics of Pointer zArray and pointer yusing array and pointer interchangeably yPassing array(pointer) to functions zDynamic memory

Passing Array to Function

EX:what’s the result?

const int size = 5 ;

void arr_mul2(int *, int);

main() {

int a[size] = {1,2,3,4,5};

arr_mul2(&a[1], size-1);

arr_mul2(a+2, size-1);

}

void arr_mul2(int* p, int n) { …… }

Page 13: Pointers. Contents zBasics of Pointer zArray and pointer yusing array and pointer interchangeably yPassing array(pointer) to functions zDynamic memory

Passing Arrays to Functions: Parameter-list Declaration

float arr_mul2(int* , int n) ;// prototypefloat arr_mul2(int *p, int n){ …… } //define

float arr_mul2(int[] , int n) ;// prototypefloat arr_mul2(int p[], int n){ …… } //define

float arr_mul2(int* , int n) ;// prototypefloat arr_mul2(int p[], int n){ …… } //define

float arr_mul2(int[] , int n) ;// prototypefloat arr_mul2(int *p, int n){ …… } //define

Page 14: Pointers. Contents zBasics of Pointer zArray and pointer yusing array and pointer interchangeably yPassing array(pointer) to functions zDynamic memory

Dynamic Memory Allocation

#include <iostream.h>#include <stdlib.h>void main() {

int *p;p = (int *)calloc(10,sizeof(int));for (int i = 0; i<=9; i++)

p[i] = i ;}

Why dynamic memory allocation?

Page 15: Pointers. Contents zBasics of Pointer zArray and pointer yusing array and pointer interchangeably yPassing array(pointer) to functions zDynamic memory

Dynamic Memory Allocation

Allocation:C: calloc(), malloc() C++: new

De-allocation:C: free

free(p);C++: delete

Page 16: Pointers. Contents zBasics of Pointer zArray and pointer yusing array and pointer interchangeably yPassing array(pointer) to functions zDynamic memory

New

void main() { // C version int *p, *q, *r ; p = (int *)malloc(sizeof(int)); q = (int *)calloc(10,sizeof(int)); r = (int *)malloc(sizeof(int)); *r=10; }

void main() { // C++ version int *p, *q, *r ; p = new int ; //allocate one q = new int[10] ; //allocate array r = new int(10) ; //allocate&initialize}

Page 17: Pointers. Contents zBasics of Pointer zArray and pointer yusing array and pointer interchangeably yPassing array(pointer) to functions zDynamic memory

Delete

void main() { int *p, *q, *r ;p = new int ;q = new int[10] ;r = new int(10) ;

…… delete p; // free one elementdelete []q; // free an arraydelete r;

}

Page 18: Pointers. Contents zBasics of Pointer zArray and pointer yusing array and pointer interchangeably yPassing array(pointer) to functions zDynamic memory

Dynamic Memory Allocation

why? Advantages are …..

// int a[10] ;int *p ;int size ;cin >> size;p = new int[size];…….

Page 19: Pointers. Contents zBasics of Pointer zArray and pointer yusing array and pointer interchangeably yPassing array(pointer) to functions zDynamic memory

Array of Pointers

char *sname[50]; // how about char sname[50][20];

s[0]

s[49]

char *sname[50], s[100] ;for (int i = 0 ; i<50; i++) {

cin >> s ;sname[i] = new char[strlen(s)+1];strcpy(sname,s);

}

Page 20: Pointers. Contents zBasics of Pointer zArray and pointer yusing array and pointer interchangeably yPassing array(pointer) to functions zDynamic memory

Array of Pointer

char **sname ; // pointer to pointerint sno; char s[100];

cin >> sno;sname = new char*[sno];

for (int i = 0 ; i<sno; i++) {cin >> s ;sname[i] = new char[strlen(s)+1];strcpy(sname,s);

}

Page 21: Pointers. Contents zBasics of Pointer zArray and pointer yusing array and pointer interchangeably yPassing array(pointer) to functions zDynamic memory

String(char *): pointer to char

void main() {char s1[10] = “hello” ;char s2[] = “hello” ;char *s3 = “hello”;char s4[] = {‘h’,’e’,’l’,’l’,’o’} ;// difference among s1,s2, s3 and s4

}

Page 22: Pointers. Contents zBasics of Pointer zArray and pointer yusing array and pointer interchangeably yPassing array(pointer) to functions zDynamic memory

Utility Functions of String

strcmp: 比較 if (s1 == s2) {….} // ???

strcpy: 複製 s1 = s2 ; // ???

strcat: 連結 s1 = s1+s2; // ???

strlen: strlen(s1); // not including ‘\0’

[NOTE]: must include <string.h>

Page 23: Pointers. Contents zBasics of Pointer zArray and pointer yusing array and pointer interchangeably yPassing array(pointer) to functions zDynamic memory

String

#include <string.h>

void main() {

char s1[] = "Hello";

char s2[] = "C++" ;

char s3[20];

if (strcmp(s1,s2) != 0) {

strcpy(s3,s1) ;

strcat(s3,s2);

}

cout << s3;

}

Page 24: Pointers. Contents zBasics of Pointer zArray and pointer yusing array and pointer interchangeably yPassing array(pointer) to functions zDynamic memory

Self Test

int fun(const char* s1, const char* s2) {

int i ;for (i = 0 ; s1[i] && s2[i] && (s1[i]==s2[i]); ++i) ; // when to exit?return (s1[i]-s2[i]) ;

}

Page 25: Pointers. Contents zBasics of Pointer zArray and pointer yusing array and pointer interchangeably yPassing array(pointer) to functions zDynamic memory

ANSI C++: string type

#include <iostream>#include <string>using namespace std;void main() {

string s1 = "Hello”, s2 = “World” ;string s3 ;s3 = s1 + " " + s2 ;cout << s3 << endl; cout << s3.length() << endl;if (s1 > s2) cout << s1 ;else cout << s2 ;

}

Page 26: Pointers. Contents zBasics of Pointer zArray and pointer yusing array and pointer interchangeably yPassing array(pointer) to functions zDynamic memory

Reference Declaration- pointer free

void main() {int x=5 ;int *p = x ;int& xx = x ; // alias of x

xx = *p + 2; cout << x ;xx++ ;*p++ ;cout << x ;

}

(*p)++;

Page 27: Pointers. Contents zBasics of Pointer zArray and pointer yusing array and pointer interchangeably yPassing array(pointer) to functions zDynamic memory

Reference to Array Elements

void main() {int x[5]= {1,2,3,4,5} ;int& first = x[0] ;int& last = x[4] ;

first *= 2 ;cout << x[0] << endl ;last = first + 2;cout << x[4] << endl;

}

Page 28: Pointers. Contents zBasics of Pointer zArray and pointer yusing array and pointer interchangeably yPassing array(pointer) to functions zDynamic memory

Call by Reference

void change(int *, int&, int ) ;void main() {

int i=5, j=6, k = 7 ;cout << i << j << k << endl;change(&i, j, k);cout << i << j << k << endl;

}void change(int* p,int& q, int r) {

q = *p + r;*p = q + r;r = *p + q;

}

Page 29: Pointers. Contents zBasics of Pointer zArray and pointer yusing array and pointer interchangeably yPassing array(pointer) to functions zDynamic memory

Reference to pointer

int x = 1, y = 2;

int *p = &x;

int*& pp = p ; // alias of p

(*p)++; cout << x << y << endl;

pp = &y ;

(*p)++ ; cout << x << y << endl;

Page 30: Pointers. Contents zBasics of Pointer zArray and pointer yusing array and pointer interchangeably yPassing array(pointer) to functions zDynamic memory

Chapter 4 Implementing ADTs Using Base Language

- final review for C-base features- prepare marching to Class

Page 31: Pointers. Contents zBasics of Pointer zArray and pointer yusing array and pointer interchangeably yPassing array(pointer) to functions zDynamic memory

Built-in Aggregate Data Type

Array

Enumeration

Structure

Union: self-reading

Page 32: Pointers. Contents zBasics of Pointer zArray and pointer yusing array and pointer interchangeably yPassing array(pointer) to functions zDynamic memory

Array

Array of build-in data type int a[10], double x[100]; int n=10; char a[n]; // not ok! const int n =10; char a[n] ; // ok

Array of composite date type struct complex { int x, y; }; complex x[100] ;

Page 33: Pointers. Contents zBasics of Pointer zArray and pointer yusing array and pointer interchangeably yPassing array(pointer) to functions zDynamic memory

Enumeration

(1) Declare a type of a subset of integer(2) Each element with a name

// type name: test// range: 0-3// name of each element: // Spring(0), Summer(1), Fall(2), Winter(3)enum test {Spring, Summer, Fall, Winter} ;main() {

test x ;if (input == ‘s’) x = Spring ; // x = 0x = 0 ; x = 50; // illegal

}

Page 34: Pointers. Contents zBasics of Pointer zArray and pointer yusing array and pointer interchangeably yPassing array(pointer) to functions zDynamic memory

Enumeration

enum season {Spring=1, Summer, Fall, Winter};enum tbound {lb = 18, avg=25, ub=38} ; main() {

season w_type ;tbound b ;….if (b >=lb && b <=avg)

w_type = Spring ;….

}

Page 35: Pointers. Contents zBasics of Pointer zArray and pointer yusing array and pointer interchangeably yPassing array(pointer) to functions zDynamic memory

Structure

nested structure

pointer to structure dynamic memory allocation

passing structure to function by value by reference & pointer