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

  • View
    234

  • Download
    12

Embed Size (px)

Text of Pointers. Contents zBasics of Pointer zArray and pointer yusing array and pointer interchangeably...

  • Pointers

  • ContentsBasics of PointerArray and pointerusing array and pointer interchangeablyPassing array(pointer) to functionsDynamic memory allocationString and pointerReference vs. pointer

  • Pointer- variables storing memory address

    Intimately tied to array and stringOperators*: deference or indirection operator, e.g. *p&: address operator, e.g. &x

  • Pointer: Declaration and Usagevoid main() {int i = 5; // Q: i? How?// A: pint *p = &i ;// Q: pi?// A: * operatorcout
  • Pointer: Declaration and Usagevoid main() {int i=5, j=6; int *p = &i ;// Q: p i?// A: yes*p = *p + 12;// A:pp = &j ;j = *p + 2; cout
  • Pointer vs. Array

    C/C++ArrayPointer

  • Array Name Pointer to First Elementmain() {int a[5] = {1,2,3,4,5};cout
  • Array Name Constant Pointermain() {int a[5] = {1,2,3,4,5};int *p;p = a ; // p = &a[0] // p a[]for (int i=0;i
  • Insight Pointer Arithmetic1600a[0] a[1] a[2] a[3] a[4]Addr2345int a[5] = {1,2,3,4,5};p = a ;p++ ; p+= 2;char a[5] = {a,b,c,d};p = a ; p+= 3;cout
  • Using Array and Pointer Interchangeablymain() {int a[size] = {1,2,3,4,5};int *p, i ;for (p=a, i=0 ;i
  • PassingArray to Functionconst 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
  • PassingArray to FunctionEX:whats 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 Declarationfloat arr_mul2(int* , int n) ;// prototypefloat arr_mul2(int *p, int n){ } //definefloat arr_mul2(int[] , int n) ;// prototypefloat arr_mul2(int p[], int n){ } //definefloat arr_mul2(int* , int n) ;// prototypefloat arr_mul2(int p[], int n){ } //definefloat arr_mul2(int[] , int n) ;// prototypefloat arr_mul2(int *p, int n){ } //define

  • Dynamic Memory Allocation#include #include void main() {int *p;p = (int *)calloc(10,sizeof(int));for (int i = 0; i
  • Dynamic Memory AllocationAllocation:C: calloc(), malloc() C++: new

    De-allocation:C: freefree(p);C++: delete

  • Newvoid 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}

  • Deletevoid 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;}

  • Dynamic Memory Allocationwhy? Advantages are ..// int a[10] ;int *p ;int size ;cin >> size;p = new int[size];.

  • Array of Pointerschar *sname[50]; // how about char sname[50][20];s[0]s[49]char *sname[50], s[100] ;for (int i = 0 ; i> s ;sname[i] = new char[strlen(s)+1];strcpy(sname,s);}

  • Array of Pointerchar **sname ; // pointer to pointerint sno; char s[100];

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

    for (int i = 0 ; i> 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 Stringstrcmp: if (s1 == s2) {.} // ???strcpy: s1 = s2 ; // ???strcat: s1 = s1+s2; // ???strlen: strlen(s1); // not including \0[NOTE]: must include

  • String#include void main() {char s1[] = "Hello";char s2[] = "C++" ;char s3[20];if (strcmp(s1,s2) != 0) {strcpy(s3,s1) ;strcat(s3,s2);}cout
  • Self Testint 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 #include using namespace std;void main() {string s1 = "Hello, s2 = World ;string s3 ;s3 = s1 + " " + s2 ;cout
  • Reference Declaration- pointer freevoid main() {int x=5 ;int *p = x ;int& xx = x ; // alias of x

    xx = *p + 2; cout

  • Reference to Array Elementsvoid main() {int x[5]= {1,2,3,4,5} ;int& first = x[0] ;int& last = x[4] ;

    first *= 2 ;cout

  • Call by Referencevoid change(int *, int&, int ) ;void main() {int i=5, j=6, k = 7 ;cout
  • Reference to pointerint x = 1, y = 2;int *p = &x;int*& pp = p ; // alias of p(*p)++; cout
  • Chapter 4 Implementing ADTs Using Base Language- final review for C-base features- prepare marching to Class

  • Built-in Aggregate Data TypeArray

    Enumeration

    Structure

    Union: self-reading

  • ArrayArray of build-in data typeint a[10], double x[100];int n=10; char a[n]; // not ok!const int n =10; char a[n] ; // okArray of composite date typestruct 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 = 0x = 0 ; x = 50; // illegal}

  • Enumerationenum season {Spring=1, Summer, Fall, Winter};enum tbound {lb = 18, avg=25, ub=38} ; main() {season w_type ;tbound b ;.if (b >=lb && b
  • Structurenested structure

    pointer to structuredynamic memory allocation

    passing structure to functionby valueby reference & pointer