Learning point C language (22): Data Type-multidimensional array and pointer

Source: Internet
Author: User
1. About the first address of the array:
 
# Include <stdio. h> int main (void) {char CS [2] [3] ={{ 'A', 'B', 'C'}, {'D ', 'E', 'F' }}; char * P1, * P2, * P3, * P4; P1 = P2 = P3 = P4 = NULL; /* the following four pointers all point to the same address */P1 = & CS [0] [0];/* this is best understood */P2 = & CS [0]; p3 = & Cs; P4 = cs;/* this is the most convenient */printf ("% P \ n", P1, p2, P3, P4);/* display address */printf ("\ n % C \ n", * P1, * P2, * P3, * P4);/* display content */getchar (); Return 0 ;}

  

2. Address of other elements in the array:

In this example, the elements of the array should be arranged in the memory as follows:

[0] [0] [0] [1] [0] [2] [1] [0] [1] [1] [1] [2]

The following is the third element of the array obtained through a pointer:

# Include <stdio. h> int main (void) {int Nums [2] [3] ={{ 11, 12, 13}, {21, 22, 23 }}; int * P1, * P2; P1 = P2 = NULL; P1 = & Nums [0] [2]; P2 = Nums; P2 = P2 + 2; // P2 = (int *) nums + 2;/* or use this sentence to replace the above two rows */printf ("% d, % d \ n", * P1, * P2); getchar (); return 0 ;}

  

3. Common Methods for Traversing Arrays:

 
# Include <stdio. h> int main (void) {int Nums [2] [3] ={{ 11, 12, 13}, {21, 22, 23 }}; int I, J; for (I = 0; I

4. traverse the array by pointer:

# Include <stdio. h> int main (void) {int Nums [2] [3] = {11, 12, 13}, {21, 22, 23 }}; int * P = Nums; int I; for (I = 0; I
 
# Include <stdio. h> int main (void) {char CS [2] [3] ={{ 'A', 'B', 'C'}, {'D ', 'E', 'F' }}; char * P = cs; unsigned I; for (I = 0; I
 
# Include <stdio. h> int main (void) {char CS [2] [3] ={{ 'A', 'B', 'C'}, {'D ', 'E', 'F' }}; int I; for (I = 0; I

5. Explore the pointer address of the array again:

# Include <stdio. h> int main (void) {char CS [2] [3] ={{ 'A', 'B', 'C'}, {'D ', 'E', 'F'}; // in this example (two-dimensional array) // CS is the address pointing to the array CS [0] // * cs is the address pointing to CS [0] [0] printf ("% P, % P \ n ", CS, * cs); // ** CS points to the value of printf ("% C, % C \ n", ** CS, CS [0] [0]); getchar (); Return 0 ;}

  

6. Use pointers to traverse 3D Arrays:

 
# Include <stdio. h> int main (void) {char CS [2] [2] [3] ={{ 'A', 'B', 'C '}, {'D', 'E', 'F' }},{ {'1', '2', '3'}, {'4', '5 ', '6' }}; int I; int COUNT = sizeof CS/sizeof CS [0] [0] [0]; for (I = 0; I

7. It is easier to understand how to traverse multi-dimensional arrays or Use Pointer variables:

 
# Include <stdio. h> int main (void) {char CS [2] [2] [3] ={{ 'A', 'B', 'C '}, {'D', 'E', 'F' }},{ {'1', '2', '3'}, {'4', '5 ', '6' }}; char * P = (char *) Cs;/* compared to the above example, type conversion is added here; in this way, the compiler does not prompt */int I; int COUNT = sizeof CS/sizeof CS [0] [0] [0]; for (I = 0; I

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.