Deep understanding of C pointer Reading Notes, deep understanding of pointers
Chapter4.h
# Ifndef _ CHAPTER_4 _ # define _ CHAPTER_4 _/* Study Notes for understanding the C pointer in depth-Chapter 4 * // * pointer array-this is an array, the pointer */void _ pointers_array_test () is stored in this array;/* contact between two-dimensional arrays and pointers */void _ two_dimension_array_test (); /* continuous memory and discontinuous memory of two-dimensional arrays */void _ two_dimension_memory_test ();/* irregular array * // void _ jarged_array_test (); # endif
Chapter. cpp
# Include "Chapter4.h" # include <stdio. h> # include <malloc. h>/* pointer array -- This indicates an array, which stores the pointer */void _ pointers_array_test () {int * test1 [5]; int I; for (I = 0; I! = 5; ++ I) {/* every element in the array is a pointer. Therefore, you need to apply a certain amount of memory before using them */test1 [I] = (int *) malloc (sizeof (int);/* obtain the value pointed to by the pointer by unreferencing the pointer */* (test1 [I]) = I + 1 ;} for (I = 0; I! = 5; ++ I) printf ("% d \ t", * (test1 + I);/* gets another method for getting the value pointed to by the pointer, the first time a pointer is referenced, the second time it is referenced, the value pointed to by the pointer */printf ("\ n") is obtained ");} void _ two_dimension_array_test () {int test1 [2] [5] = {1, 2, 3, 4, 5}, {6, 7, 8, 9, 10 }}; int I, j; for (I = 0; I! = 2; ++ I) {for (j = 0; j! = 5; ++ j) {printf ("test1 [% d] [% d] address is: % x, value is: % d \ n", I, j, & test1 [I] [j], test1 [I] [j]) ;}} printf ("\ n"); printf ("test1 address is: % x \ n ", test1); printf (" test1 + 1 address is: % x \ n ", test1 + 1);/* the output result of the program is as follows: test1 [0] [0] address is: 3df780, value is: 1test1 [0] [1] address is: 3df784, value is: 2test1 [0] [2] address is: 3df788, value is: 3test1 [0] [3] address is: 3df78c, value is: 4te St1 [0] [4] address is: 3df790, value is: 5test1 [1] [0] address is: 3df794, value is: 6test1 [1] [1] address is: 3df798, value is: 7test1 [1] [2] address is: 3df79c, value is: 8test1 [1] [3] address is: 3df7a0, value is: 9test1 [1] [4] address is: 3df7a4, value is: 10test1 address is: 3df780test1 + 1 address is: 3df794 you can see, the output address of the Two-dimensional array name is the starting address of the first element. Add 1 to the array name. The result is (original address + number of columns * element size) */} void _ t Wo_dimension_memory_test () {/* the memory address of the elements in the two-dimensional array must be continuous */int test1 [2] [2] = {1, 2}, {3, 4 }}; int I, j; for (I = 0; I! = 2; ++ I) {for (j = 0; j! = 2; ++ j) {printf ("test1 [% d] [% d] address is: % x \ n", I, j, & test1 [I] [j]) ;}/ * the output result is as follows: test1 [0] [0] address is: 32f9a0test1 [0] [1] address is: 32f9a4test1 [1] [0] address is: 32f9a8test1 [1] [1] address is: 32f9ac */printf ("\ n "); /* The Element Memory of the dynamic two-dimensional array is not necessarily continuous, because two malloc functions */int ** test2 = (int **) are called **) malloc (sizeof (int *) * 2); for (I = 0; I! = 2; ++ I) test2 [I] = (int *) malloc (sizeof (int) * 2); for (I = 0; I! = 2; ++ I) {for (j = 0; j! = 2; ++ j) {printf ("test2 [% d] [% d] address is: % x \ n", I, j, & test2 [I] [j]) ;}/ * the output result is as follows: test2 [0] [0] address is: 67a8f8test2 [0] [1] address is: 67a8fctest2 [1] [0] address is: 67a930test2 [1] [1] address is: 67a934 */printf ("\ n "); /* Two memory allocation methods make the elements in the array allocated by the dynamic two-dimensional array continuous */int rows = 2; int cols = 2; /* method 1 * // * apply for memory for each row */int ** test3 = (int **) malloc (sizeof (int *) * rows ); /* the memory required for applying all elements at one time. Therefore, the obtained memory is continuous */test3 [0] = (I Nt *) malloc (sizeof (int) * rows * cols); for (I = 1; I! = Rows; ++ I) test3 [I] = test3 [0] + I * cols; for (I = 0; I! = Rows; ++ I) {for (j = 0; j! = Cols; ++ j) {printf ("test3 [% d] [% d] address is: % x \ n", I, j, & test3 [I] [j]) ;}/ * the output result is as follows: test3 [0] [0] address is: 5fa9a0test3 [0] [1] address is: 5fa9a4test3 [1] [0] address is: 5fa9a8test3 [1] [1] address is: 5fa9ac */printf ("\ n "); /* method 2 * // * in the form of a one-dimensional array, the disadvantage of applying for all memory at a time * // * is that, we cannot use subscript to reference elements in the array, because this method loses the two-dimensional array form that the compiler needs to know */int * test4 = (int *) malloc (sizeof (int) * rows * cols); for (I = 0; I! = Rows; ++ I) {for (j = 0; j! = Cols; ++ j) {printf ("test4 [% d] [% d] address is: % x \ n", I, j, test4 + (I * cols) + j) ;}}/* output result: test4 [0] [0] address is: 34a9e0test4 [0] [1] address is: 34a9e4test4 [1] [0] address is: 34a9e8test4 [1] [1] address is: 34a9ec */}/* irregular array * // void _ jarged_array_test () // {/* create a two-dimensional array with a composite literal * // * int (* (test1 []) = {(int []) {0, 1, 2 }, (int []) {3, 4, 5}, (int []) {6, 7, 8 }};*///}