350 likes | 375 Views
Pointers. Contents. Basics of Pointer Array and pointer using array and pointer interchangeably Passing array(pointer) to functions Dynamic memory allocation String and pointer Reference vs. pointer. Pointer. - v ariables storing memory address Intimately tied to array and string
E N D
Contents • Basics of Pointer • Array and pointer • using array and pointer interchangeably • Passing array(pointer) to functions • Dynamic memory allocation • String and pointer • Reference vs. pointer
Pointer - variables storing memory address • Intimately tied to array and string • Operators • *: deference or indirection operator, e.g. *p • &: address operator, e.g. &x
Pointer: Declaration and Usage void main() { int i = 5; // Q: 想儲存i的位址? How? // A: 宣告一個指標 p int *p = &i ; // Q: 如何利用p取出i的值? // A: 使用 * operator cout << *p ; // Q: 我可以印出i的位址(p的值)? // A: it’s OK!, 如下: cout << p; }
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; }
Pointer vs. Array 在C/C++程式寫作上,Array和Pointer好像雙 生兄弟,經常交換使用。
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; }
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++;} }
601 604 602 608 603 612 604 616 p p p p Insight Pointer Arithmetic a[0] a[1] a[2] a[3] a[4] int a[5] = {1,2,3,4,5}; p = a ; p++ ; p+= 2; 1 2 3 4 5 Addr 600 a[0] a[1] a[2] a[3] a[4] char a[5] = {‘a’,’b’,’c’,’d’}; p = a ; p+= 3; cout << *p; a b c d Addr 600
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]; }
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; }
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) { …… }
Passing Arrays to Functions : Parameter-list Declaration float arr_mul2(int* , int n) ;// prototype float arr_mul2(int *p, int n){ …… } //define float arr_mul2(int[] , int n) ;// prototype float arr_mul2(int p[], int n){ …… } //define float arr_mul2(int* , int n) ;// prototype float arr_mul2(int p[], int n){ …… } //define float arr_mul2(int[] , int n) ;// prototype float arr_mul2(int *p, int n){ …… } //define
Dynamic Memory Allocation Why 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 ; }
Dynamic Memory Allocation Allocation: C: calloc(), malloc() C++: new De-allocation: C: free free(p); C++: delete
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 }
Delete void main() { int *p, *q, *r ; p = new int ; q = new int[10] ; r = new int(10) ; …… delete p; // free one element delete []q; // free an array delete r; }
Dynamic Memory Allocation • why? Advantages are ….. // int a[10] ; int *p ; int size ; cin >> size; p = new int[size]; …….
Array of Pointers char *sname[50]; // how about char sname[50][20]; s[0] char *sname[50], s[100] ; for (int i = 0 ; i<50; i++) { cin >> s ; sname[i] = new char[strlen(s)+1]; strcpy(sname,s); } s[49]
Array of Pointer char **sname ; // pointer to pointer int 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); }
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 }
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>
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; }
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]) ; }
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 ; }
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)++;
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; }
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; }
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;
Chapter 4 Implementing ADTs Using Base Language - final review for C-base features - prepare marching to Class
Built-in Aggregate Data Type • Array • Enumeration • Structure • Union: self-reading
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] ;
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 = 0 x = 0 ; x = 50; // illegal }
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 ; …. }
Structure • nested structure • pointer to structure • dynamic memory allocation • passing structure to function • by value • by reference & pointer