180 likes | 340 Vues
This lecture notes explore the intricate concepts of pointers and multidimensional arrays in C++. Key topics include the use of void pointers, dynamic memory allocation, and the difference between arrays of pointers and arrays of strings. Examples demonstrate how to initialize and manipulate multidimensional arrays. The notes also provide practical code snippets for declaring arrays, handling pointers, and managing dynamic memory effectively. The aim is to deepen comprehension of these essential programming concepts in C++.
E N D
CS 192 Lecture 13 Winter 2003 January 5-6, 2004 Dr. Shafay Shamail
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
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
Initializing Multi-Dimentional Arrays static int example[5][3] = { 1, 2, 3, 4, 5, 6, 7 }; 1 2 3 4 5 6 7 0 0 0 0 0 0 0 0 static int example[5][3] = { { 1, 2, 3 }, { 4 }, { 5, 6, 7 } }; 1 2 3 4 0 0 5 6 7 0 0 0 0 0 0
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
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
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; }
Array of Strings vs. Array of Pointers • 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 • 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”};
Dynamic Allocation The new operator int *x_ptr = new int; OR int *xptr; xptr = new int; //heap
Dynamic Allocation int *xptr=new int; *xptr = 73; int *x2ptr = new int; *x2ptr = 65;
Dynamic Allocation What is wrong here? int *xptr = new int; *xptr = 73; int *x2ptr; *x2ptr=65;
Dynamic Allocation int *xptr = new int; *xptr = 73; int *x2ptr; x2ptr = xptr; *x2ptr = 65;
Dynamic Allocation //What is wrong here? int *xptr = new int; *xptr = 73; int *x2ptr = new int; x2ptr = xptr; *x2ptr = 65; //memory leak
Dynamic Allocation int *myptr = new int(73); cout << *myptr; delete myptr;
Dynamic Allocation 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;
0 1 2 3 4 5 iarray 0 1 2 3 Array of Pointers A two dimensional array of RSIZE*CSIZE 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); } }
300 200 100 20 ippp ipp ip a 400 300 200 100 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 100 20 200 100 20 300 200 100 20
Dynamic Array of Pointers A two dimensional array of RSIZE*CSIZE 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); } } matrix 0 1 2 3 4 5 0 1 2 3