Introduction Multi-Dimensional array is always interesting to explore in different languages, since they are the core of scientific computing. The example code below will guide you for this tour. Implementation - 1. 1D array with pointer int * ptr = new int [num]; #include <iostream> using namespace std; int main() { // 1d pointer int *ptr = new int[3]; cout << *(ptr+1) << " " << ptr << endl; cout << "1d over "<< endl; delete ptr; =========================================================================== === Print Out === ================= 0 0xa66010 // this 0xa66010 is value of ptr without referencing id over =========================================================================== // explain: // -- // 1. ptr is the address // 2. ptr[0] does not mean (*ptr)[0], it is illegal. // ptr[0] = *(ptr+0) 2. 2D array with pointer int** ptr = new int* [num]; // 2d pointer int **p = new int*[3]; int i,j; cout << "p before fully initialized " << p << endl; cout << " the pointer below is not fully initialized, so it gives 0 as INIT-Value " << endl; for (i = 0; i < 3; i++ ) { cout << " |address/value of header-row pointer it belongs| --> " << *(p+i) <<endl; } =========================================================================== === Print Out === ================= p before fully initialized 0xa66010 the pointer below is not fully initialized, so it gives 0 as INIT-Value |address/value of header-row pointer it belongs| --> 0 |address/value of header-row pointer it belongs| --> 0 |address/value of header-row pointer it belongs| --> 0 p after fully initialized 0xa66010 // it means, p is unchanged 0 // but *(p+i) changes after fully initialized =========================================================================== for (i = 0; i < 3; i++ ) { p[i] = new int[4]; } cout << "p after fully initialized " << p << endl; cout << **p << endl; for (i = 0; i < 3; i++ ) { cout << " == " << endl; cout << "p:" << p << endl; cout << *p+i << " " << p[i] << endl; cout << "first stride: " << sizeof(int) << endl; cout << " -- " << endl; } == p:0xa66010 // still p is not changed 0xa66030 0xa66030 // the first one: (*p)+i // the second one: *(p+i) {also = p[i]} // pointer + 1, always means to the next stride location // but *(p+1), if p is a pointer as well, then it will jump to the next "row". Keep in mind that 2D array is still a 1D array in terms of memory address. first stride: 4 -- == p:0xa66010 0xa66034 0xa66050 first stride: 4 -- == p:0xa66010 0xa66038 0xa66070 first stride: 4 -- // show the 2d for (i=0; i<3;i++) { for (j=0; j<4;j++) cout << p[i][j] << " value from pointers-law --> " << *(*(p+i)+j) << " |address/value of header-row pointer it belongs| --> "<< *(p+i) << " |address/value of integer it belongs| --> " << *(p+i)+j << endl; } for (i = 0; i < 3; i++ ) { delete[] p[i]; // delete the pointer pointed to array, you need delete[] } // explain: // -- // 1. *(p+i) = p[i]: means 'i-th element of a array of pointers', intotal there are 3 element for this array // 2. p is only making sense after fully initialized, which means *p ~= an address delete[] p; // same as above, p points to the address of row-head of 2D array // =========================================================================== === Print Out === ================= 0 value from pointers-law --> 0 |address/value of header-row pointer it belongs| --> 0x9ca030 |address/value of integer it belongs| --> 0x9ca030 0 value from pointers-law --> 0 |address/value of header-row pointer it belongs| --> 0x9ca030 |address/value of integer it belongs| --> 0x9ca034 0 value from pointers-law --> 0 |address/value of header-row pointer it belongs| --> 0x9ca030 |address/value of integer it belongs| --> 0x9ca038 0 value from pointers-law --> 0 |address/value of header-row pointer it belongs| --> 0x9ca030 |address/value of integer it belongs| --> 0x9ca03c 0 value from pointers-law --> 0 |address/value of header-row pointer it belongs| --> 0x9ca050 |address/value of integer it belongs| --> 0x9ca050 0 value from pointers-law --> 0 |address/value of header-row pointer it belongs| --> 0x9ca050 |address/value of integer it belongs| --> 0x9ca054 0 value from pointers-law --> 0 |address/value of header-row pointer it belongs| --> 0x9ca050 |address/value of integer it belongs| --> 0x9ca058 0 value from pointers-law --> 0 |address/value of header-row pointer it belongs| --> 0x9ca050 |address/value of integer it belongs| --> 0x9ca05c 0 value from pointers-law --> 0 |address/value of header-row pointer it belongs| --> 0x9ca070 |address/value of integer it belongs| --> 0x9ca070 0 value from pointers-law --> 0 |address/value of header-row pointer it belongs| --> 0x9ca070 |address/value of integer it belongs| --> 0x9ca074 0 value from pointers-law --> 0 |address/value of header-row pointer it belongs| --> 0x9ca070 |address/value of integer it belongs| --> 0x9ca078 0 value from pointers-law --> 0 |address/value of header-row pointer it belongs| --> 0x9ca070 |address/value of integer it belongs| --> 0x9ca07c ============================================================================ } Important notes
|
AuthorShaowu Pan Archives
December 2017
Categories
All
|