Pointer
Introduction to this section:
In the previous section we resolved the arrays in the C language composite data type, in this section we will
Focus, C language Soul-----pointers to learn! The advantage of using pointers: using pointers to represent and use complex data structures;
More convenient use of our arrays and strings; You can handle memory cell addresses directly as assembly language, and you can dynamically make memory space
allocation, C language pointer is the focus, but also the difficulty of C, let everyone follow the footsteps of the author, c in the hands of the analysis of the!
Learning Roadmap for this section:
pointer concepts, pointers vs. one-dimensional arrays and strings:
pointers and two-dimensional arrays:
This knowledge point is a pointer to the difficulty, in fact, to understand here, we just put the center of gravity on the two-dimensional array, the two-dimensional array to thoroughly understand!
Step 1: two-dimensional arrays store data in rows and columns; we can look at a number of lines + one-dimensional array of several columns per row
int a[3][4] ={{1,2,3,4},{1,2,3,4},{1,2,3,4}}; Can be viewed as three rows with four one-dimensional arrays per row
Step 2:a represents the address of the first element of a two-dimensional array, that is, &a[0], if the second and three lines of the newline array are only required : A + 1,a+2 ; &a[1],&a[2]
Step 3: We can think of a[0] as a pointer to a[0][0] , then a[0]+1 points to A[0][1], that is, the address at this time is &A[0][1]
PS: +1 of the 1 here is the number of bytes of the array elements, such as int a[], then this + 1 means to move backwards 4 bytes, pointing to the next element;
If it's +2, it's 8 bytes behind, so a[i][2] = A[i] + 2
Step 4: Through the previous one-dimensional array learning, we can know A[i] and * (a+i) equivalent
Similarly, we extend to a two-dimensional array of cases A[i][j] and * (a+i) +j and a[i]+j are equivalent, both represent A[I][J] addresses
the value of A[i][j] : * (A[I]+J) and * (* (a+i) +j) are OK
Note :*a and * (a+i) do not represent any specific array elements, so you may cause errors when quoting them!
① Array First address a[0]: A can not be directly referenced !!!
② one-dimensional array a[0] The address of the first dollar (&a[0][0]): a[0],* (a+0), *a;
③ First line address: A + 1 also cannot be applied directly
④ one-dimensional array a[1] The address of the first dollar (&a[1][1]): a[1],* (A + 1)
⑤ number 2 row 4 column element a[2][4] address:a[2]+4,* (a+2) +4,a[2][4]
code example:
① points to an array element:
Run:
② pointing to an array
Run:
Summary:
① pointer concept, pointer variable, direct access and indirect access
Basic use of ② pointers
③ pointers and one-dimensional arrays
④ Pointers and strings
⑤ pointers and two-dimensional arrays
This paper draws on: http://blog.csdn.net/coder_pig/article/details/37754625
Copyright NOTICE: Welcome reprint, Hope in your reprint at the same time, add the original address, thank you with
C (5)