Presentation is loading. Please wait.

Presentation is loading. Please wait.

CS 192 Lecture 13 Winter 2003 January 5-6, 2004 Dr. Shafay Shamail.

Similar presentations


Presentation on theme: "CS 192 Lecture 13 Winter 2003 January 5-6, 2004 Dr. Shafay Shamail."— Presentation transcript:

1 CS 192 Lecture 13 Winter 2003 January 5-6, 2004 Dr. Shafay Shamail

2 Void Pointer int age = 20; int *ageptr = &age; void *vptr = (void *)ageptr; int *newptr = (int *)vptr; Can’t do: vptr++ vptr— vptr + 2 As the base type of vptr is not known

3 Pointers and Multidimensional Arrays int ar[2][3] = { { 0, 1, 2 }, { 3, 4, 5 } }; ar[1][2]= *(ar[1] + 2) = *(*(ar+1) + 2) = *(int *)((char *)ar + (1*3*4) + (2*4)) = *(int *)((char *)ar + 20) Where 3 = # of elements in each row And each int is of 4 byte long

4 Initializing Multi-Dimentional Arrays static int example[5][3] = { { 1, 2, 3 }, { 4 }, { 5, 6, 7 } }; 1 2 3 4 0 0 5 6 7 0 0 0 static int example[5][3] = { 1, 2, 3, 4, 5, 6, 7 }; 1 2 3 4 5 6 7 0 0 0 0 0

5 Initializing Multi-Dimensional Arrays static int example[][3][2] = { { {1, 1}, {2, 2}, {3,3} }, { {4, 4}, {5, 5}, {6,6} }, { {1, 1}, {2, 2}, {3,3} }, { {4, 4}, {5, 5}, {6,6} } }; 4-by-3-by-2 array

6 Arrays of Pointers char *fortunes[] = { "Soon, you will come into some money.\n", "A new love will enter your life.\n", "You will live long and prosper.\n", "Now is a good time to invest for the future.\n", "A close friend will ask for a favor.\n" }; cout << fortunes[1] << endl << fortunes<<endl << *(fortunes) << endl << *(fortunes[2]) << endl; Output: A new love will enter your life 0012FFC6 Soon, you will come into some money Y

7 Arrays of Pointers … Two dimensional string pointers e.g. C++ dictionary char *keyword[][2] = { "for", "for(initialization; condition; increment)", "if", "if(condition)... else...", "switch", "switch(value) { case-list }", "while", "while(condition)...", // add the rest of the C++ keywords here "", "" // terminate the list with nulls }; int main() { char str[80]; int i; cout << "Enter keyword: "; cin >> str; for(i=0; *keyword[i][0]; i++) if(!strcmp(keyword[i][0], str)) cout << keyword[i][1]; return 0; }

8 Array of Strings vs. Array of Pointers  char movies[5][20] = {“Godfather”, “Maula Jatt”, “A Fish Called Wanda”, “Blade Runner”, “Spiderman”};  char *movies[5] = {“Godfather”, “Maula Jatt”, “A Fish Called Wanda”, “Blade Runner”, “Spiderman”}; each column is 20 characters wide due to the longest movie name wasted space the strings are placed contiguously in memory without wasting space pointers in the array movie point to them

9 The new operator int *x_ptr = new int; OR int *xptr; xptr = new int; //heap Dynamic Allocation

10 int *xptr=new int; *xptr = 73; int *x2ptr = new int; *x2ptr = 65; Dynamic Allocation

11 What is wrong here? int *xptr = new int; *xptr = 73; int *x2ptr; *x2ptr=65; Dynamic Allocation

12 int *xptr = new int; *xptr = 73; int *x2ptr; x2ptr = xptr; *x2ptr = 65; Dynamic Allocation

13 //What is wrong here? int *xptr = new int; *xptr = 73; int *x2ptr = new int; x2ptr = xptr; *x2ptr = 65; //memory leak Dynamic Allocation

14 int *myptr = new int(73); cout << *myptr; delete myptr; Dynamic Allocation

15 const int SIZE = 10; double *ptr = new double[SIZE]; /*10 element array*/ int i; for (i=0; i<SIZE; i++) { ptr[i] = 2.0*i; } for (i=0; i<SIZE; i++) { cout << ptr[i] << endl; } delete []ptr; Dynamic Allocation

16 Array of Pointers const int RSIZE = 4; const int CSIZE = 6; int * iarray[RSIZE]; for(int k=0; k<RSIZE; k++) { iarray[k] = new int [CSIZE]; if (iarray[k] == NULL) { cerr << “Memory Allocation Error…\n”; cerr << “Exiting …\n”; exit(-1); } A two dimensional array of RSIZE*CSIZE iarray 01230123 0 1 2 3 4 5

17 Double and Tripple Pointers int a = 20; int *ip = &a; int **ipp = &ip; int ***ippp = &ipp; cout << a << endl; cout << ip << *ip << endl; cout << ipp << *ipp << **ipp << endl; cout << ippp << *ippp << **ippp << ***ippp << endl; 20 a 100 ip 200 ipp 300 ippp 400 20 100 20 200 100 20 300 200 100 20

18 Dynamic Array of Pointers const int RSIZE = 4; const int CSIZE = 6; int ** matrix; matrix = new int * [RSIZE]; for(int k=0; k<RSIZE; k++) { matrix[k] = new int [CSIZE]; if (matrix[k] == NULL) { cerr << “Memory Allocation Error…\n”; cerr << “Exiting …\n”; exit(-1); } A two dimensional array of RSIZE*CSIZE 01230123 0 1 2 3 4 5 matrix


Download ppt "CS 192 Lecture 13 Winter 2003 January 5-6, 2004 Dr. Shafay Shamail."

Similar presentations


Ads by Google